region.c 87 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /* Copyright(c) 2022 Intel Corporation. All rights reserved. */
  3. #include <linux/memregion.h>
  4. #include <linux/genalloc.h>
  5. #include <linux/device.h>
  6. #include <linux/module.h>
  7. #include <linux/memory.h>
  8. #include <linux/slab.h>
  9. #include <linux/uuid.h>
  10. #include <linux/sort.h>
  11. #include <linux/idr.h>
  12. #include <linux/memory-tiers.h>
  13. #include <cxlmem.h>
  14. #include <cxl.h>
  15. #include "core.h"
  16. /**
  17. * DOC: cxl core region
  18. *
  19. * CXL Regions represent mapped memory capacity in system physical address
  20. * space. Whereas the CXL Root Decoders identify the bounds of potential CXL
  21. * Memory ranges, Regions represent the active mapped capacity by the HDM
  22. * Decoder Capability structures throughout the Host Bridges, Switches, and
  23. * Endpoints in the topology.
  24. *
  25. * Region configuration has ordering constraints. UUID may be set at any time
  26. * but is only visible for persistent regions.
  27. * 1. Interleave granularity
  28. * 2. Interleave size
  29. * 3. Decoder targets
  30. */
  31. static struct cxl_region *to_cxl_region(struct device *dev);
  32. #define __ACCESS_ATTR_RO(_level, _name) { \
  33. .attr = { .name = __stringify(_name), .mode = 0444 }, \
  34. .show = _name##_access##_level##_show, \
  35. }
  36. #define ACCESS_DEVICE_ATTR_RO(level, name) \
  37. struct device_attribute dev_attr_access##level##_##name = __ACCESS_ATTR_RO(level, name)
  38. #define ACCESS_ATTR_RO(level, attrib) \
  39. static ssize_t attrib##_access##level##_show(struct device *dev, \
  40. struct device_attribute *attr, \
  41. char *buf) \
  42. { \
  43. struct cxl_region *cxlr = to_cxl_region(dev); \
  44. \
  45. if (cxlr->coord[level].attrib == 0) \
  46. return -ENOENT; \
  47. \
  48. return sysfs_emit(buf, "%u\n", cxlr->coord[level].attrib); \
  49. } \
  50. static ACCESS_DEVICE_ATTR_RO(level, attrib)
  51. ACCESS_ATTR_RO(0, read_bandwidth);
  52. ACCESS_ATTR_RO(0, read_latency);
  53. ACCESS_ATTR_RO(0, write_bandwidth);
  54. ACCESS_ATTR_RO(0, write_latency);
  55. #define ACCESS_ATTR_DECLARE(level, attrib) \
  56. (&dev_attr_access##level##_##attrib.attr)
  57. static struct attribute *access0_coordinate_attrs[] = {
  58. ACCESS_ATTR_DECLARE(0, read_bandwidth),
  59. ACCESS_ATTR_DECLARE(0, write_bandwidth),
  60. ACCESS_ATTR_DECLARE(0, read_latency),
  61. ACCESS_ATTR_DECLARE(0, write_latency),
  62. NULL
  63. };
  64. ACCESS_ATTR_RO(1, read_bandwidth);
  65. ACCESS_ATTR_RO(1, read_latency);
  66. ACCESS_ATTR_RO(1, write_bandwidth);
  67. ACCESS_ATTR_RO(1, write_latency);
  68. static struct attribute *access1_coordinate_attrs[] = {
  69. ACCESS_ATTR_DECLARE(1, read_bandwidth),
  70. ACCESS_ATTR_DECLARE(1, write_bandwidth),
  71. ACCESS_ATTR_DECLARE(1, read_latency),
  72. ACCESS_ATTR_DECLARE(1, write_latency),
  73. NULL
  74. };
  75. #define ACCESS_VISIBLE(level) \
  76. static umode_t cxl_region_access##level##_coordinate_visible( \
  77. struct kobject *kobj, struct attribute *a, int n) \
  78. { \
  79. struct device *dev = kobj_to_dev(kobj); \
  80. struct cxl_region *cxlr = to_cxl_region(dev); \
  81. \
  82. if (a == &dev_attr_access##level##_read_latency.attr && \
  83. cxlr->coord[level].read_latency == 0) \
  84. return 0; \
  85. \
  86. if (a == &dev_attr_access##level##_write_latency.attr && \
  87. cxlr->coord[level].write_latency == 0) \
  88. return 0; \
  89. \
  90. if (a == &dev_attr_access##level##_read_bandwidth.attr && \
  91. cxlr->coord[level].read_bandwidth == 0) \
  92. return 0; \
  93. \
  94. if (a == &dev_attr_access##level##_write_bandwidth.attr && \
  95. cxlr->coord[level].write_bandwidth == 0) \
  96. return 0; \
  97. \
  98. return a->mode; \
  99. }
  100. ACCESS_VISIBLE(0);
  101. ACCESS_VISIBLE(1);
  102. static const struct attribute_group cxl_region_access0_coordinate_group = {
  103. .name = "access0",
  104. .attrs = access0_coordinate_attrs,
  105. .is_visible = cxl_region_access0_coordinate_visible,
  106. };
  107. static const struct attribute_group *get_cxl_region_access0_group(void)
  108. {
  109. return &cxl_region_access0_coordinate_group;
  110. }
  111. static const struct attribute_group cxl_region_access1_coordinate_group = {
  112. .name = "access1",
  113. .attrs = access1_coordinate_attrs,
  114. .is_visible = cxl_region_access1_coordinate_visible,
  115. };
  116. static const struct attribute_group *get_cxl_region_access1_group(void)
  117. {
  118. return &cxl_region_access1_coordinate_group;
  119. }
  120. static ssize_t uuid_show(struct device *dev, struct device_attribute *attr,
  121. char *buf)
  122. {
  123. struct cxl_region *cxlr = to_cxl_region(dev);
  124. struct cxl_region_params *p = &cxlr->params;
  125. ssize_t rc;
  126. rc = down_read_interruptible(&cxl_region_rwsem);
  127. if (rc)
  128. return rc;
  129. if (cxlr->mode != CXL_DECODER_PMEM)
  130. rc = sysfs_emit(buf, "\n");
  131. else
  132. rc = sysfs_emit(buf, "%pUb\n", &p->uuid);
  133. up_read(&cxl_region_rwsem);
  134. return rc;
  135. }
  136. static int is_dup(struct device *match, void *data)
  137. {
  138. struct cxl_region_params *p;
  139. struct cxl_region *cxlr;
  140. uuid_t *uuid = data;
  141. if (!is_cxl_region(match))
  142. return 0;
  143. lockdep_assert_held(&cxl_region_rwsem);
  144. cxlr = to_cxl_region(match);
  145. p = &cxlr->params;
  146. if (uuid_equal(&p->uuid, uuid)) {
  147. dev_dbg(match, "already has uuid: %pUb\n", uuid);
  148. return -EBUSY;
  149. }
  150. return 0;
  151. }
  152. static ssize_t uuid_store(struct device *dev, struct device_attribute *attr,
  153. const char *buf, size_t len)
  154. {
  155. struct cxl_region *cxlr = to_cxl_region(dev);
  156. struct cxl_region_params *p = &cxlr->params;
  157. uuid_t temp;
  158. ssize_t rc;
  159. if (len != UUID_STRING_LEN + 1)
  160. return -EINVAL;
  161. rc = uuid_parse(buf, &temp);
  162. if (rc)
  163. return rc;
  164. if (uuid_is_null(&temp))
  165. return -EINVAL;
  166. rc = down_write_killable(&cxl_region_rwsem);
  167. if (rc)
  168. return rc;
  169. if (uuid_equal(&p->uuid, &temp))
  170. goto out;
  171. rc = -EBUSY;
  172. if (p->state >= CXL_CONFIG_ACTIVE)
  173. goto out;
  174. rc = bus_for_each_dev(&cxl_bus_type, NULL, &temp, is_dup);
  175. if (rc < 0)
  176. goto out;
  177. uuid_copy(&p->uuid, &temp);
  178. out:
  179. up_write(&cxl_region_rwsem);
  180. if (rc)
  181. return rc;
  182. return len;
  183. }
  184. static DEVICE_ATTR_RW(uuid);
  185. static struct cxl_region_ref *cxl_rr_load(struct cxl_port *port,
  186. struct cxl_region *cxlr)
  187. {
  188. return xa_load(&port->regions, (unsigned long)cxlr);
  189. }
  190. static int cxl_region_invalidate_memregion(struct cxl_region *cxlr)
  191. {
  192. if (!cpu_cache_has_invalidate_memregion()) {
  193. if (IS_ENABLED(CONFIG_CXL_REGION_INVALIDATION_TEST)) {
  194. dev_info_once(
  195. &cxlr->dev,
  196. "Bypassing cpu_cache_invalidate_memregion() for testing!\n");
  197. return 0;
  198. } else {
  199. dev_WARN(&cxlr->dev,
  200. "Failed to synchronize CPU cache state\n");
  201. return -ENXIO;
  202. }
  203. }
  204. cpu_cache_invalidate_memregion(IORES_DESC_CXL);
  205. return 0;
  206. }
  207. static void cxl_region_decode_reset(struct cxl_region *cxlr, int count)
  208. {
  209. struct cxl_region_params *p = &cxlr->params;
  210. int i;
  211. /*
  212. * Before region teardown attempt to flush, evict any data cached for
  213. * this region, or scream loudly about missing arch / platform support
  214. * for CXL teardown.
  215. */
  216. cxl_region_invalidate_memregion(cxlr);
  217. for (i = count - 1; i >= 0; i--) {
  218. struct cxl_endpoint_decoder *cxled = p->targets[i];
  219. struct cxl_memdev *cxlmd = cxled_to_memdev(cxled);
  220. struct cxl_port *iter = cxled_to_port(cxled);
  221. struct cxl_dev_state *cxlds = cxlmd->cxlds;
  222. struct cxl_ep *ep;
  223. if (cxlds->rcd)
  224. goto endpoint_reset;
  225. while (!is_cxl_root(to_cxl_port(iter->dev.parent)))
  226. iter = to_cxl_port(iter->dev.parent);
  227. for (ep = cxl_ep_load(iter, cxlmd); iter;
  228. iter = ep->next, ep = cxl_ep_load(iter, cxlmd)) {
  229. struct cxl_region_ref *cxl_rr;
  230. struct cxl_decoder *cxld;
  231. cxl_rr = cxl_rr_load(iter, cxlr);
  232. cxld = cxl_rr->decoder;
  233. if (cxld->reset)
  234. cxld->reset(cxld);
  235. set_bit(CXL_REGION_F_NEEDS_RESET, &cxlr->flags);
  236. }
  237. endpoint_reset:
  238. cxled->cxld.reset(&cxled->cxld);
  239. set_bit(CXL_REGION_F_NEEDS_RESET, &cxlr->flags);
  240. }
  241. /* all decoders associated with this region have been torn down */
  242. clear_bit(CXL_REGION_F_NEEDS_RESET, &cxlr->flags);
  243. }
  244. static int commit_decoder(struct cxl_decoder *cxld)
  245. {
  246. struct cxl_switch_decoder *cxlsd = NULL;
  247. if (cxld->commit)
  248. return cxld->commit(cxld);
  249. if (is_switch_decoder(&cxld->dev))
  250. cxlsd = to_cxl_switch_decoder(&cxld->dev);
  251. if (dev_WARN_ONCE(&cxld->dev, !cxlsd || cxlsd->nr_targets > 1,
  252. "->commit() is required\n"))
  253. return -ENXIO;
  254. return 0;
  255. }
  256. static int cxl_region_decode_commit(struct cxl_region *cxlr)
  257. {
  258. struct cxl_region_params *p = &cxlr->params;
  259. int i, rc = 0;
  260. for (i = 0; i < p->nr_targets; i++) {
  261. struct cxl_endpoint_decoder *cxled = p->targets[i];
  262. struct cxl_memdev *cxlmd = cxled_to_memdev(cxled);
  263. struct cxl_region_ref *cxl_rr;
  264. struct cxl_decoder *cxld;
  265. struct cxl_port *iter;
  266. struct cxl_ep *ep;
  267. /* commit bottom up */
  268. for (iter = cxled_to_port(cxled); !is_cxl_root(iter);
  269. iter = to_cxl_port(iter->dev.parent)) {
  270. cxl_rr = cxl_rr_load(iter, cxlr);
  271. cxld = cxl_rr->decoder;
  272. rc = commit_decoder(cxld);
  273. if (rc)
  274. break;
  275. }
  276. if (rc) {
  277. /* programming @iter failed, teardown */
  278. for (ep = cxl_ep_load(iter, cxlmd); ep && iter;
  279. iter = ep->next, ep = cxl_ep_load(iter, cxlmd)) {
  280. cxl_rr = cxl_rr_load(iter, cxlr);
  281. cxld = cxl_rr->decoder;
  282. if (cxld->reset)
  283. cxld->reset(cxld);
  284. }
  285. cxled->cxld.reset(&cxled->cxld);
  286. goto err;
  287. }
  288. }
  289. return 0;
  290. err:
  291. /* undo the targets that were successfully committed */
  292. cxl_region_decode_reset(cxlr, i);
  293. return rc;
  294. }
  295. static ssize_t commit_store(struct device *dev, struct device_attribute *attr,
  296. const char *buf, size_t len)
  297. {
  298. struct cxl_region *cxlr = to_cxl_region(dev);
  299. struct cxl_region_params *p = &cxlr->params;
  300. bool commit;
  301. ssize_t rc;
  302. rc = kstrtobool(buf, &commit);
  303. if (rc)
  304. return rc;
  305. rc = down_write_killable(&cxl_region_rwsem);
  306. if (rc)
  307. return rc;
  308. /* Already in the requested state? */
  309. if (commit && p->state >= CXL_CONFIG_COMMIT)
  310. goto out;
  311. if (!commit && p->state < CXL_CONFIG_COMMIT)
  312. goto out;
  313. /* Not ready to commit? */
  314. if (commit && p->state < CXL_CONFIG_ACTIVE) {
  315. rc = -ENXIO;
  316. goto out;
  317. }
  318. /*
  319. * Invalidate caches before region setup to drop any speculative
  320. * consumption of this address space
  321. */
  322. rc = cxl_region_invalidate_memregion(cxlr);
  323. if (rc)
  324. goto out;
  325. if (commit) {
  326. rc = cxl_region_decode_commit(cxlr);
  327. if (rc == 0)
  328. p->state = CXL_CONFIG_COMMIT;
  329. } else {
  330. p->state = CXL_CONFIG_RESET_PENDING;
  331. up_write(&cxl_region_rwsem);
  332. device_release_driver(&cxlr->dev);
  333. down_write(&cxl_region_rwsem);
  334. /*
  335. * The lock was dropped, so need to revalidate that the reset is
  336. * still pending.
  337. */
  338. if (p->state == CXL_CONFIG_RESET_PENDING) {
  339. cxl_region_decode_reset(cxlr, p->interleave_ways);
  340. p->state = CXL_CONFIG_ACTIVE;
  341. }
  342. }
  343. out:
  344. up_write(&cxl_region_rwsem);
  345. if (rc)
  346. return rc;
  347. return len;
  348. }
  349. static ssize_t commit_show(struct device *dev, struct device_attribute *attr,
  350. char *buf)
  351. {
  352. struct cxl_region *cxlr = to_cxl_region(dev);
  353. struct cxl_region_params *p = &cxlr->params;
  354. ssize_t rc;
  355. rc = down_read_interruptible(&cxl_region_rwsem);
  356. if (rc)
  357. return rc;
  358. rc = sysfs_emit(buf, "%d\n", p->state >= CXL_CONFIG_COMMIT);
  359. up_read(&cxl_region_rwsem);
  360. return rc;
  361. }
  362. static DEVICE_ATTR_RW(commit);
  363. static umode_t cxl_region_visible(struct kobject *kobj, struct attribute *a,
  364. int n)
  365. {
  366. struct device *dev = kobj_to_dev(kobj);
  367. struct cxl_region *cxlr = to_cxl_region(dev);
  368. /*
  369. * Support tooling that expects to find a 'uuid' attribute for all
  370. * regions regardless of mode.
  371. */
  372. if (a == &dev_attr_uuid.attr && cxlr->mode != CXL_DECODER_PMEM)
  373. return 0444;
  374. return a->mode;
  375. }
  376. static ssize_t interleave_ways_show(struct device *dev,
  377. struct device_attribute *attr, char *buf)
  378. {
  379. struct cxl_region *cxlr = to_cxl_region(dev);
  380. struct cxl_region_params *p = &cxlr->params;
  381. ssize_t rc;
  382. rc = down_read_interruptible(&cxl_region_rwsem);
  383. if (rc)
  384. return rc;
  385. rc = sysfs_emit(buf, "%d\n", p->interleave_ways);
  386. up_read(&cxl_region_rwsem);
  387. return rc;
  388. }
  389. static const struct attribute_group *get_cxl_region_target_group(void);
  390. static ssize_t interleave_ways_store(struct device *dev,
  391. struct device_attribute *attr,
  392. const char *buf, size_t len)
  393. {
  394. struct cxl_root_decoder *cxlrd = to_cxl_root_decoder(dev->parent);
  395. struct cxl_decoder *cxld = &cxlrd->cxlsd.cxld;
  396. struct cxl_region *cxlr = to_cxl_region(dev);
  397. struct cxl_region_params *p = &cxlr->params;
  398. unsigned int val, save;
  399. int rc;
  400. u8 iw;
  401. rc = kstrtouint(buf, 0, &val);
  402. if (rc)
  403. return rc;
  404. rc = ways_to_eiw(val, &iw);
  405. if (rc)
  406. return rc;
  407. /*
  408. * Even for x3, x6, and x12 interleaves the region interleave must be a
  409. * power of 2 multiple of the host bridge interleave.
  410. */
  411. if (!is_power_of_2(val / cxld->interleave_ways) ||
  412. (val % cxld->interleave_ways)) {
  413. dev_dbg(&cxlr->dev, "invalid interleave: %d\n", val);
  414. return -EINVAL;
  415. }
  416. rc = down_write_killable(&cxl_region_rwsem);
  417. if (rc)
  418. return rc;
  419. if (p->state >= CXL_CONFIG_INTERLEAVE_ACTIVE) {
  420. rc = -EBUSY;
  421. goto out;
  422. }
  423. save = p->interleave_ways;
  424. p->interleave_ways = val;
  425. rc = sysfs_update_group(&cxlr->dev.kobj, get_cxl_region_target_group());
  426. if (rc)
  427. p->interleave_ways = save;
  428. out:
  429. up_write(&cxl_region_rwsem);
  430. if (rc)
  431. return rc;
  432. return len;
  433. }
  434. static DEVICE_ATTR_RW(interleave_ways);
  435. static ssize_t interleave_granularity_show(struct device *dev,
  436. struct device_attribute *attr,
  437. char *buf)
  438. {
  439. struct cxl_region *cxlr = to_cxl_region(dev);
  440. struct cxl_region_params *p = &cxlr->params;
  441. ssize_t rc;
  442. rc = down_read_interruptible(&cxl_region_rwsem);
  443. if (rc)
  444. return rc;
  445. rc = sysfs_emit(buf, "%d\n", p->interleave_granularity);
  446. up_read(&cxl_region_rwsem);
  447. return rc;
  448. }
  449. static ssize_t interleave_granularity_store(struct device *dev,
  450. struct device_attribute *attr,
  451. const char *buf, size_t len)
  452. {
  453. struct cxl_root_decoder *cxlrd = to_cxl_root_decoder(dev->parent);
  454. struct cxl_decoder *cxld = &cxlrd->cxlsd.cxld;
  455. struct cxl_region *cxlr = to_cxl_region(dev);
  456. struct cxl_region_params *p = &cxlr->params;
  457. int rc, val;
  458. u16 ig;
  459. rc = kstrtoint(buf, 0, &val);
  460. if (rc)
  461. return rc;
  462. rc = granularity_to_eig(val, &ig);
  463. if (rc)
  464. return rc;
  465. /*
  466. * When the host-bridge is interleaved, disallow region granularity !=
  467. * root granularity. Regions with a granularity less than the root
  468. * interleave result in needing multiple endpoints to support a single
  469. * slot in the interleave (possible to support in the future). Regions
  470. * with a granularity greater than the root interleave result in invalid
  471. * DPA translations (invalid to support).
  472. */
  473. if (cxld->interleave_ways > 1 && val != cxld->interleave_granularity)
  474. return -EINVAL;
  475. rc = down_write_killable(&cxl_region_rwsem);
  476. if (rc)
  477. return rc;
  478. if (p->state >= CXL_CONFIG_INTERLEAVE_ACTIVE) {
  479. rc = -EBUSY;
  480. goto out;
  481. }
  482. p->interleave_granularity = val;
  483. out:
  484. up_write(&cxl_region_rwsem);
  485. if (rc)
  486. return rc;
  487. return len;
  488. }
  489. static DEVICE_ATTR_RW(interleave_granularity);
  490. static ssize_t resource_show(struct device *dev, struct device_attribute *attr,
  491. char *buf)
  492. {
  493. struct cxl_region *cxlr = to_cxl_region(dev);
  494. struct cxl_region_params *p = &cxlr->params;
  495. u64 resource = -1ULL;
  496. ssize_t rc;
  497. rc = down_read_interruptible(&cxl_region_rwsem);
  498. if (rc)
  499. return rc;
  500. if (p->res)
  501. resource = p->res->start;
  502. rc = sysfs_emit(buf, "%#llx\n", resource);
  503. up_read(&cxl_region_rwsem);
  504. return rc;
  505. }
  506. static DEVICE_ATTR_RO(resource);
  507. static ssize_t mode_show(struct device *dev, struct device_attribute *attr,
  508. char *buf)
  509. {
  510. struct cxl_region *cxlr = to_cxl_region(dev);
  511. return sysfs_emit(buf, "%s\n", cxl_decoder_mode_name(cxlr->mode));
  512. }
  513. static DEVICE_ATTR_RO(mode);
  514. static int alloc_hpa(struct cxl_region *cxlr, resource_size_t size)
  515. {
  516. struct cxl_root_decoder *cxlrd = to_cxl_root_decoder(cxlr->dev.parent);
  517. struct cxl_region_params *p = &cxlr->params;
  518. struct resource *res;
  519. u64 remainder = 0;
  520. lockdep_assert_held_write(&cxl_region_rwsem);
  521. /* Nothing to do... */
  522. if (p->res && resource_size(p->res) == size)
  523. return 0;
  524. /* To change size the old size must be freed first */
  525. if (p->res)
  526. return -EBUSY;
  527. if (p->state >= CXL_CONFIG_INTERLEAVE_ACTIVE)
  528. return -EBUSY;
  529. /* ways, granularity and uuid (if PMEM) need to be set before HPA */
  530. if (!p->interleave_ways || !p->interleave_granularity ||
  531. (cxlr->mode == CXL_DECODER_PMEM && uuid_is_null(&p->uuid)))
  532. return -ENXIO;
  533. div64_u64_rem(size, (u64)SZ_256M * p->interleave_ways, &remainder);
  534. if (remainder)
  535. return -EINVAL;
  536. res = alloc_free_mem_region(cxlrd->res, size, SZ_256M,
  537. dev_name(&cxlr->dev));
  538. if (IS_ERR(res)) {
  539. dev_dbg(&cxlr->dev,
  540. "HPA allocation error (%ld) for size:%pap in %s %pr\n",
  541. PTR_ERR(res), &size, cxlrd->res->name, cxlrd->res);
  542. return PTR_ERR(res);
  543. }
  544. p->res = res;
  545. p->state = CXL_CONFIG_INTERLEAVE_ACTIVE;
  546. return 0;
  547. }
  548. static void cxl_region_iomem_release(struct cxl_region *cxlr)
  549. {
  550. struct cxl_region_params *p = &cxlr->params;
  551. if (device_is_registered(&cxlr->dev))
  552. lockdep_assert_held_write(&cxl_region_rwsem);
  553. if (p->res) {
  554. /*
  555. * Autodiscovered regions may not have been able to insert their
  556. * resource.
  557. */
  558. if (p->res->parent)
  559. remove_resource(p->res);
  560. kfree(p->res);
  561. p->res = NULL;
  562. }
  563. }
  564. static int free_hpa(struct cxl_region *cxlr)
  565. {
  566. struct cxl_region_params *p = &cxlr->params;
  567. lockdep_assert_held_write(&cxl_region_rwsem);
  568. if (!p->res)
  569. return 0;
  570. if (p->state >= CXL_CONFIG_ACTIVE)
  571. return -EBUSY;
  572. cxl_region_iomem_release(cxlr);
  573. p->state = CXL_CONFIG_IDLE;
  574. return 0;
  575. }
  576. static ssize_t size_store(struct device *dev, struct device_attribute *attr,
  577. const char *buf, size_t len)
  578. {
  579. struct cxl_region *cxlr = to_cxl_region(dev);
  580. u64 val;
  581. int rc;
  582. rc = kstrtou64(buf, 0, &val);
  583. if (rc)
  584. return rc;
  585. rc = down_write_killable(&cxl_region_rwsem);
  586. if (rc)
  587. return rc;
  588. if (val)
  589. rc = alloc_hpa(cxlr, val);
  590. else
  591. rc = free_hpa(cxlr);
  592. up_write(&cxl_region_rwsem);
  593. if (rc)
  594. return rc;
  595. return len;
  596. }
  597. static ssize_t size_show(struct device *dev, struct device_attribute *attr,
  598. char *buf)
  599. {
  600. struct cxl_region *cxlr = to_cxl_region(dev);
  601. struct cxl_region_params *p = &cxlr->params;
  602. u64 size = 0;
  603. ssize_t rc;
  604. rc = down_read_interruptible(&cxl_region_rwsem);
  605. if (rc)
  606. return rc;
  607. if (p->res)
  608. size = resource_size(p->res);
  609. rc = sysfs_emit(buf, "%#llx\n", size);
  610. up_read(&cxl_region_rwsem);
  611. return rc;
  612. }
  613. static DEVICE_ATTR_RW(size);
  614. static struct attribute *cxl_region_attrs[] = {
  615. &dev_attr_uuid.attr,
  616. &dev_attr_commit.attr,
  617. &dev_attr_interleave_ways.attr,
  618. &dev_attr_interleave_granularity.attr,
  619. &dev_attr_resource.attr,
  620. &dev_attr_size.attr,
  621. &dev_attr_mode.attr,
  622. NULL,
  623. };
  624. static const struct attribute_group cxl_region_group = {
  625. .attrs = cxl_region_attrs,
  626. .is_visible = cxl_region_visible,
  627. };
  628. static size_t show_targetN(struct cxl_region *cxlr, char *buf, int pos)
  629. {
  630. struct cxl_region_params *p = &cxlr->params;
  631. struct cxl_endpoint_decoder *cxled;
  632. int rc;
  633. rc = down_read_interruptible(&cxl_region_rwsem);
  634. if (rc)
  635. return rc;
  636. if (pos >= p->interleave_ways) {
  637. dev_dbg(&cxlr->dev, "position %d out of range %d\n", pos,
  638. p->interleave_ways);
  639. rc = -ENXIO;
  640. goto out;
  641. }
  642. cxled = p->targets[pos];
  643. if (!cxled)
  644. rc = sysfs_emit(buf, "\n");
  645. else
  646. rc = sysfs_emit(buf, "%s\n", dev_name(&cxled->cxld.dev));
  647. out:
  648. up_read(&cxl_region_rwsem);
  649. return rc;
  650. }
  651. static int check_commit_order(struct device *dev, const void *data)
  652. {
  653. struct cxl_decoder *cxld = to_cxl_decoder(dev);
  654. /*
  655. * if port->commit_end is not the only free decoder, then out of
  656. * order shutdown has occurred, block further allocations until
  657. * that is resolved
  658. */
  659. if (((cxld->flags & CXL_DECODER_F_ENABLE) == 0))
  660. return -EBUSY;
  661. return 0;
  662. }
  663. static int match_free_decoder(struct device *dev, void *data)
  664. {
  665. struct cxl_port *port = to_cxl_port(dev->parent);
  666. struct cxl_decoder *cxld;
  667. int rc;
  668. if (!is_switch_decoder(dev))
  669. return 0;
  670. cxld = to_cxl_decoder(dev);
  671. if (cxld->id != port->commit_end + 1)
  672. return 0;
  673. if (cxld->region) {
  674. dev_dbg(dev->parent,
  675. "next decoder to commit (%s) is already reserved (%s)\n",
  676. dev_name(dev), dev_name(&cxld->region->dev));
  677. return 0;
  678. }
  679. rc = device_for_each_child_reverse_from(dev->parent, dev, NULL,
  680. check_commit_order);
  681. if (rc) {
  682. dev_dbg(dev->parent,
  683. "unable to allocate %s due to out of order shutdown\n",
  684. dev_name(dev));
  685. return 0;
  686. }
  687. return 1;
  688. }
  689. static int match_auto_decoder(struct device *dev, void *data)
  690. {
  691. struct cxl_region_params *p = data;
  692. struct cxl_decoder *cxld;
  693. struct range *r;
  694. if (!is_switch_decoder(dev))
  695. return 0;
  696. cxld = to_cxl_decoder(dev);
  697. r = &cxld->hpa_range;
  698. if (p->res && p->res->start == r->start && p->res->end == r->end)
  699. return 1;
  700. return 0;
  701. }
  702. static struct cxl_decoder *
  703. cxl_region_find_decoder(struct cxl_port *port,
  704. struct cxl_endpoint_decoder *cxled,
  705. struct cxl_region *cxlr)
  706. {
  707. struct device *dev;
  708. if (port == cxled_to_port(cxled))
  709. return &cxled->cxld;
  710. if (test_bit(CXL_REGION_F_AUTO, &cxlr->flags))
  711. dev = device_find_child(&port->dev, &cxlr->params,
  712. match_auto_decoder);
  713. else
  714. dev = device_find_child(&port->dev, NULL, match_free_decoder);
  715. if (!dev)
  716. return NULL;
  717. /*
  718. * This decoder is pinned registered as long as the endpoint decoder is
  719. * registered, and endpoint decoder unregistration holds the
  720. * cxl_region_rwsem over unregister events, so no need to hold on to
  721. * this extra reference.
  722. */
  723. put_device(dev);
  724. return to_cxl_decoder(dev);
  725. }
  726. static bool auto_order_ok(struct cxl_port *port, struct cxl_region *cxlr_iter,
  727. struct cxl_decoder *cxld)
  728. {
  729. struct cxl_region_ref *rr = cxl_rr_load(port, cxlr_iter);
  730. struct cxl_decoder *cxld_iter = rr->decoder;
  731. /*
  732. * Allow the out of order assembly of auto-discovered regions.
  733. * Per CXL Spec 3.1 8.2.4.20.12 software must commit decoders
  734. * in HPA order. Confirm that the decoder with the lesser HPA
  735. * starting address has the lesser id.
  736. */
  737. dev_dbg(&cxld->dev, "check for HPA violation %s:%d < %s:%d\n",
  738. dev_name(&cxld->dev), cxld->id,
  739. dev_name(&cxld_iter->dev), cxld_iter->id);
  740. if (cxld_iter->id > cxld->id)
  741. return true;
  742. return false;
  743. }
  744. static struct cxl_region_ref *
  745. alloc_region_ref(struct cxl_port *port, struct cxl_region *cxlr,
  746. struct cxl_endpoint_decoder *cxled)
  747. {
  748. struct cxl_region_params *p = &cxlr->params;
  749. struct cxl_region_ref *cxl_rr, *iter;
  750. unsigned long index;
  751. int rc;
  752. xa_for_each(&port->regions, index, iter) {
  753. struct cxl_region_params *ip = &iter->region->params;
  754. if (!ip->res || ip->res->start < p->res->start)
  755. continue;
  756. if (test_bit(CXL_REGION_F_AUTO, &cxlr->flags)) {
  757. struct cxl_decoder *cxld;
  758. cxld = cxl_region_find_decoder(port, cxled, cxlr);
  759. if (auto_order_ok(port, iter->region, cxld))
  760. continue;
  761. }
  762. dev_dbg(&cxlr->dev, "%s: HPA order violation %s:%pr vs %pr\n",
  763. dev_name(&port->dev),
  764. dev_name(&iter->region->dev), ip->res, p->res);
  765. return ERR_PTR(-EBUSY);
  766. }
  767. cxl_rr = kzalloc(sizeof(*cxl_rr), GFP_KERNEL);
  768. if (!cxl_rr)
  769. return ERR_PTR(-ENOMEM);
  770. cxl_rr->port = port;
  771. cxl_rr->region = cxlr;
  772. cxl_rr->nr_targets = 1;
  773. xa_init(&cxl_rr->endpoints);
  774. rc = xa_insert(&port->regions, (unsigned long)cxlr, cxl_rr, GFP_KERNEL);
  775. if (rc) {
  776. dev_dbg(&cxlr->dev,
  777. "%s: failed to track region reference: %d\n",
  778. dev_name(&port->dev), rc);
  779. kfree(cxl_rr);
  780. return ERR_PTR(rc);
  781. }
  782. return cxl_rr;
  783. }
  784. static void cxl_rr_free_decoder(struct cxl_region_ref *cxl_rr)
  785. {
  786. struct cxl_region *cxlr = cxl_rr->region;
  787. struct cxl_decoder *cxld = cxl_rr->decoder;
  788. if (!cxld)
  789. return;
  790. dev_WARN_ONCE(&cxlr->dev, cxld->region != cxlr, "region mismatch\n");
  791. if (cxld->region == cxlr) {
  792. cxld->region = NULL;
  793. put_device(&cxlr->dev);
  794. }
  795. }
  796. static void free_region_ref(struct cxl_region_ref *cxl_rr)
  797. {
  798. struct cxl_port *port = cxl_rr->port;
  799. struct cxl_region *cxlr = cxl_rr->region;
  800. cxl_rr_free_decoder(cxl_rr);
  801. xa_erase(&port->regions, (unsigned long)cxlr);
  802. xa_destroy(&cxl_rr->endpoints);
  803. kfree(cxl_rr);
  804. }
  805. static int cxl_rr_ep_add(struct cxl_region_ref *cxl_rr,
  806. struct cxl_endpoint_decoder *cxled)
  807. {
  808. int rc;
  809. struct cxl_port *port = cxl_rr->port;
  810. struct cxl_region *cxlr = cxl_rr->region;
  811. struct cxl_decoder *cxld = cxl_rr->decoder;
  812. struct cxl_ep *ep = cxl_ep_load(port, cxled_to_memdev(cxled));
  813. if (ep) {
  814. rc = xa_insert(&cxl_rr->endpoints, (unsigned long)cxled, ep,
  815. GFP_KERNEL);
  816. if (rc)
  817. return rc;
  818. }
  819. cxl_rr->nr_eps++;
  820. if (!cxld->region) {
  821. cxld->region = cxlr;
  822. get_device(&cxlr->dev);
  823. }
  824. return 0;
  825. }
  826. static int cxl_rr_alloc_decoder(struct cxl_port *port, struct cxl_region *cxlr,
  827. struct cxl_endpoint_decoder *cxled,
  828. struct cxl_region_ref *cxl_rr)
  829. {
  830. struct cxl_decoder *cxld;
  831. cxld = cxl_region_find_decoder(port, cxled, cxlr);
  832. if (!cxld) {
  833. dev_dbg(&cxlr->dev, "%s: no decoder available\n",
  834. dev_name(&port->dev));
  835. return -EBUSY;
  836. }
  837. if (cxld->region) {
  838. dev_dbg(&cxlr->dev, "%s: %s already attached to %s\n",
  839. dev_name(&port->dev), dev_name(&cxld->dev),
  840. dev_name(&cxld->region->dev));
  841. return -EBUSY;
  842. }
  843. /*
  844. * Endpoints should already match the region type, but backstop that
  845. * assumption with an assertion. Switch-decoders change mapping-type
  846. * based on what is mapped when they are assigned to a region.
  847. */
  848. dev_WARN_ONCE(&cxlr->dev,
  849. port == cxled_to_port(cxled) &&
  850. cxld->target_type != cxlr->type,
  851. "%s:%s mismatch decoder type %d -> %d\n",
  852. dev_name(&cxled_to_memdev(cxled)->dev),
  853. dev_name(&cxld->dev), cxld->target_type, cxlr->type);
  854. cxld->target_type = cxlr->type;
  855. cxl_rr->decoder = cxld;
  856. return 0;
  857. }
  858. /**
  859. * cxl_port_attach_region() - track a region's interest in a port by endpoint
  860. * @port: port to add a new region reference 'struct cxl_region_ref'
  861. * @cxlr: region to attach to @port
  862. * @cxled: endpoint decoder used to create or further pin a region reference
  863. * @pos: interleave position of @cxled in @cxlr
  864. *
  865. * The attach event is an opportunity to validate CXL decode setup
  866. * constraints and record metadata needed for programming HDM decoders,
  867. * in particular decoder target lists.
  868. *
  869. * The steps are:
  870. *
  871. * - validate that there are no other regions with a higher HPA already
  872. * associated with @port
  873. * - establish a region reference if one is not already present
  874. *
  875. * - additionally allocate a decoder instance that will host @cxlr on
  876. * @port
  877. *
  878. * - pin the region reference by the endpoint
  879. * - account for how many entries in @port's target list are needed to
  880. * cover all of the added endpoints.
  881. */
  882. static int cxl_port_attach_region(struct cxl_port *port,
  883. struct cxl_region *cxlr,
  884. struct cxl_endpoint_decoder *cxled, int pos)
  885. {
  886. struct cxl_memdev *cxlmd = cxled_to_memdev(cxled);
  887. struct cxl_ep *ep = cxl_ep_load(port, cxlmd);
  888. struct cxl_region_ref *cxl_rr;
  889. bool nr_targets_inc = false;
  890. struct cxl_decoder *cxld;
  891. unsigned long index;
  892. int rc = -EBUSY;
  893. lockdep_assert_held_write(&cxl_region_rwsem);
  894. cxl_rr = cxl_rr_load(port, cxlr);
  895. if (cxl_rr) {
  896. struct cxl_ep *ep_iter;
  897. int found = 0;
  898. /*
  899. * Walk the existing endpoints that have been attached to
  900. * @cxlr at @port and see if they share the same 'next' port
  901. * in the downstream direction. I.e. endpoints that share common
  902. * upstream switch.
  903. */
  904. xa_for_each(&cxl_rr->endpoints, index, ep_iter) {
  905. if (ep_iter == ep)
  906. continue;
  907. if (ep_iter->next == ep->next) {
  908. found++;
  909. break;
  910. }
  911. }
  912. /*
  913. * New target port, or @port is an endpoint port that always
  914. * accounts its own local decode as a target.
  915. */
  916. if (!found || !ep->next) {
  917. cxl_rr->nr_targets++;
  918. nr_targets_inc = true;
  919. }
  920. } else {
  921. cxl_rr = alloc_region_ref(port, cxlr, cxled);
  922. if (IS_ERR(cxl_rr)) {
  923. dev_dbg(&cxlr->dev,
  924. "%s: failed to allocate region reference\n",
  925. dev_name(&port->dev));
  926. return PTR_ERR(cxl_rr);
  927. }
  928. nr_targets_inc = true;
  929. rc = cxl_rr_alloc_decoder(port, cxlr, cxled, cxl_rr);
  930. if (rc)
  931. goto out_erase;
  932. }
  933. cxld = cxl_rr->decoder;
  934. /*
  935. * the number of targets should not exceed the target_count
  936. * of the decoder
  937. */
  938. if (is_switch_decoder(&cxld->dev)) {
  939. struct cxl_switch_decoder *cxlsd;
  940. cxlsd = to_cxl_switch_decoder(&cxld->dev);
  941. if (cxl_rr->nr_targets > cxlsd->nr_targets) {
  942. dev_dbg(&cxlr->dev,
  943. "%s:%s %s add: %s:%s @ %d overflows targets: %d\n",
  944. dev_name(port->uport_dev), dev_name(&port->dev),
  945. dev_name(&cxld->dev), dev_name(&cxlmd->dev),
  946. dev_name(&cxled->cxld.dev), pos,
  947. cxlsd->nr_targets);
  948. rc = -ENXIO;
  949. goto out_erase;
  950. }
  951. }
  952. rc = cxl_rr_ep_add(cxl_rr, cxled);
  953. if (rc) {
  954. dev_dbg(&cxlr->dev,
  955. "%s: failed to track endpoint %s:%s reference\n",
  956. dev_name(&port->dev), dev_name(&cxlmd->dev),
  957. dev_name(&cxld->dev));
  958. goto out_erase;
  959. }
  960. dev_dbg(&cxlr->dev,
  961. "%s:%s %s add: %s:%s @ %d next: %s nr_eps: %d nr_targets: %d\n",
  962. dev_name(port->uport_dev), dev_name(&port->dev),
  963. dev_name(&cxld->dev), dev_name(&cxlmd->dev),
  964. dev_name(&cxled->cxld.dev), pos,
  965. ep ? ep->next ? dev_name(ep->next->uport_dev) :
  966. dev_name(&cxlmd->dev) :
  967. "none",
  968. cxl_rr->nr_eps, cxl_rr->nr_targets);
  969. return 0;
  970. out_erase:
  971. if (nr_targets_inc)
  972. cxl_rr->nr_targets--;
  973. if (cxl_rr->nr_eps == 0)
  974. free_region_ref(cxl_rr);
  975. return rc;
  976. }
  977. static void cxl_port_detach_region(struct cxl_port *port,
  978. struct cxl_region *cxlr,
  979. struct cxl_endpoint_decoder *cxled)
  980. {
  981. struct cxl_region_ref *cxl_rr;
  982. struct cxl_ep *ep = NULL;
  983. lockdep_assert_held_write(&cxl_region_rwsem);
  984. cxl_rr = cxl_rr_load(port, cxlr);
  985. if (!cxl_rr)
  986. return;
  987. /*
  988. * Endpoint ports do not carry cxl_ep references, and they
  989. * never target more than one endpoint by definition
  990. */
  991. if (cxl_rr->decoder == &cxled->cxld)
  992. cxl_rr->nr_eps--;
  993. else
  994. ep = xa_erase(&cxl_rr->endpoints, (unsigned long)cxled);
  995. if (ep) {
  996. struct cxl_ep *ep_iter;
  997. unsigned long index;
  998. int found = 0;
  999. cxl_rr->nr_eps--;
  1000. xa_for_each(&cxl_rr->endpoints, index, ep_iter) {
  1001. if (ep_iter->next == ep->next) {
  1002. found++;
  1003. break;
  1004. }
  1005. }
  1006. if (!found)
  1007. cxl_rr->nr_targets--;
  1008. }
  1009. if (cxl_rr->nr_eps == 0)
  1010. free_region_ref(cxl_rr);
  1011. }
  1012. static int check_last_peer(struct cxl_endpoint_decoder *cxled,
  1013. struct cxl_ep *ep, struct cxl_region_ref *cxl_rr,
  1014. int distance)
  1015. {
  1016. struct cxl_memdev *cxlmd = cxled_to_memdev(cxled);
  1017. struct cxl_region *cxlr = cxl_rr->region;
  1018. struct cxl_region_params *p = &cxlr->params;
  1019. struct cxl_endpoint_decoder *cxled_peer;
  1020. struct cxl_port *port = cxl_rr->port;
  1021. struct cxl_memdev *cxlmd_peer;
  1022. struct cxl_ep *ep_peer;
  1023. int pos = cxled->pos;
  1024. /*
  1025. * If this position wants to share a dport with the last endpoint mapped
  1026. * then that endpoint, at index 'position - distance', must also be
  1027. * mapped by this dport.
  1028. */
  1029. if (pos < distance) {
  1030. dev_dbg(&cxlr->dev, "%s:%s: cannot host %s:%s at %d\n",
  1031. dev_name(port->uport_dev), dev_name(&port->dev),
  1032. dev_name(&cxlmd->dev), dev_name(&cxled->cxld.dev), pos);
  1033. return -ENXIO;
  1034. }
  1035. cxled_peer = p->targets[pos - distance];
  1036. cxlmd_peer = cxled_to_memdev(cxled_peer);
  1037. ep_peer = cxl_ep_load(port, cxlmd_peer);
  1038. if (ep->dport != ep_peer->dport) {
  1039. dev_dbg(&cxlr->dev,
  1040. "%s:%s: %s:%s pos %d mismatched peer %s:%s\n",
  1041. dev_name(port->uport_dev), dev_name(&port->dev),
  1042. dev_name(&cxlmd->dev), dev_name(&cxled->cxld.dev), pos,
  1043. dev_name(&cxlmd_peer->dev),
  1044. dev_name(&cxled_peer->cxld.dev));
  1045. return -ENXIO;
  1046. }
  1047. return 0;
  1048. }
  1049. static int check_interleave_cap(struct cxl_decoder *cxld, int iw, int ig)
  1050. {
  1051. struct cxl_port *port = to_cxl_port(cxld->dev.parent);
  1052. struct cxl_hdm *cxlhdm = dev_get_drvdata(&port->dev);
  1053. unsigned int interleave_mask;
  1054. u8 eiw;
  1055. u16 eig;
  1056. int high_pos, low_pos;
  1057. if (!test_bit(iw, &cxlhdm->iw_cap_mask))
  1058. return -ENXIO;
  1059. /*
  1060. * Per CXL specification r3.1(8.2.4.20.13 Decoder Protection),
  1061. * if eiw < 8:
  1062. * DPAOFFSET[51: eig + 8] = HPAOFFSET[51: eig + 8 + eiw]
  1063. * DPAOFFSET[eig + 7: 0] = HPAOFFSET[eig + 7: 0]
  1064. *
  1065. * when the eiw is 0, all the bits of HPAOFFSET[51: 0] are used, the
  1066. * interleave bits are none.
  1067. *
  1068. * if eiw >= 8:
  1069. * DPAOFFSET[51: eig + 8] = HPAOFFSET[51: eig + eiw] / 3
  1070. * DPAOFFSET[eig + 7: 0] = HPAOFFSET[eig + 7: 0]
  1071. *
  1072. * when the eiw is 8, all the bits of HPAOFFSET[51: 0] are used, the
  1073. * interleave bits are none.
  1074. */
  1075. ways_to_eiw(iw, &eiw);
  1076. if (eiw == 0 || eiw == 8)
  1077. return 0;
  1078. granularity_to_eig(ig, &eig);
  1079. if (eiw > 8)
  1080. high_pos = eiw + eig - 1;
  1081. else
  1082. high_pos = eiw + eig + 7;
  1083. low_pos = eig + 8;
  1084. interleave_mask = GENMASK(high_pos, low_pos);
  1085. if (interleave_mask & ~cxlhdm->interleave_mask)
  1086. return -ENXIO;
  1087. return 0;
  1088. }
  1089. static int cxl_port_setup_targets(struct cxl_port *port,
  1090. struct cxl_region *cxlr,
  1091. struct cxl_endpoint_decoder *cxled)
  1092. {
  1093. struct cxl_root_decoder *cxlrd = to_cxl_root_decoder(cxlr->dev.parent);
  1094. int parent_iw, parent_ig, ig, iw, rc, inc = 0, pos = cxled->pos;
  1095. struct cxl_port *parent_port = to_cxl_port(port->dev.parent);
  1096. struct cxl_region_ref *cxl_rr = cxl_rr_load(port, cxlr);
  1097. struct cxl_memdev *cxlmd = cxled_to_memdev(cxled);
  1098. struct cxl_ep *ep = cxl_ep_load(port, cxlmd);
  1099. struct cxl_region_params *p = &cxlr->params;
  1100. struct cxl_decoder *cxld = cxl_rr->decoder;
  1101. struct cxl_switch_decoder *cxlsd;
  1102. struct cxl_port *iter = port;
  1103. u16 eig, peig;
  1104. u8 eiw, peiw;
  1105. /*
  1106. * While root level decoders support x3, x6, x12, switch level
  1107. * decoders only support powers of 2 up to x16.
  1108. */
  1109. if (!is_power_of_2(cxl_rr->nr_targets)) {
  1110. dev_dbg(&cxlr->dev, "%s:%s: invalid target count %d\n",
  1111. dev_name(port->uport_dev), dev_name(&port->dev),
  1112. cxl_rr->nr_targets);
  1113. return -EINVAL;
  1114. }
  1115. cxlsd = to_cxl_switch_decoder(&cxld->dev);
  1116. if (cxl_rr->nr_targets_set) {
  1117. int i, distance = 1;
  1118. struct cxl_region_ref *cxl_rr_iter;
  1119. /*
  1120. * The "distance" between peer downstream ports represents which
  1121. * endpoint positions in the region interleave a given port can
  1122. * host.
  1123. *
  1124. * For example, at the root of a hierarchy the distance is
  1125. * always 1 as every index targets a different host-bridge. At
  1126. * each subsequent switch level those ports map every Nth region
  1127. * position where N is the width of the switch == distance.
  1128. */
  1129. do {
  1130. cxl_rr_iter = cxl_rr_load(iter, cxlr);
  1131. distance *= cxl_rr_iter->nr_targets;
  1132. iter = to_cxl_port(iter->dev.parent);
  1133. } while (!is_cxl_root(iter));
  1134. distance *= cxlrd->cxlsd.cxld.interleave_ways;
  1135. for (i = 0; i < cxl_rr->nr_targets_set; i++)
  1136. if (ep->dport == cxlsd->target[i]) {
  1137. rc = check_last_peer(cxled, ep, cxl_rr,
  1138. distance);
  1139. if (rc)
  1140. return rc;
  1141. goto out_target_set;
  1142. }
  1143. goto add_target;
  1144. }
  1145. if (is_cxl_root(parent_port)) {
  1146. /*
  1147. * Root decoder IG is always set to value in CFMWS which
  1148. * may be different than this region's IG. We can use the
  1149. * region's IG here since interleave_granularity_store()
  1150. * does not allow interleaved host-bridges with
  1151. * root IG != region IG.
  1152. */
  1153. parent_ig = p->interleave_granularity;
  1154. parent_iw = cxlrd->cxlsd.cxld.interleave_ways;
  1155. /*
  1156. * For purposes of address bit routing, use power-of-2 math for
  1157. * switch ports.
  1158. */
  1159. if (!is_power_of_2(parent_iw))
  1160. parent_iw /= 3;
  1161. } else {
  1162. struct cxl_region_ref *parent_rr;
  1163. struct cxl_decoder *parent_cxld;
  1164. parent_rr = cxl_rr_load(parent_port, cxlr);
  1165. parent_cxld = parent_rr->decoder;
  1166. parent_ig = parent_cxld->interleave_granularity;
  1167. parent_iw = parent_cxld->interleave_ways;
  1168. }
  1169. rc = granularity_to_eig(parent_ig, &peig);
  1170. if (rc) {
  1171. dev_dbg(&cxlr->dev, "%s:%s: invalid parent granularity: %d\n",
  1172. dev_name(parent_port->uport_dev),
  1173. dev_name(&parent_port->dev), parent_ig);
  1174. return rc;
  1175. }
  1176. rc = ways_to_eiw(parent_iw, &peiw);
  1177. if (rc) {
  1178. dev_dbg(&cxlr->dev, "%s:%s: invalid parent interleave: %d\n",
  1179. dev_name(parent_port->uport_dev),
  1180. dev_name(&parent_port->dev), parent_iw);
  1181. return rc;
  1182. }
  1183. iw = cxl_rr->nr_targets;
  1184. rc = ways_to_eiw(iw, &eiw);
  1185. if (rc) {
  1186. dev_dbg(&cxlr->dev, "%s:%s: invalid port interleave: %d\n",
  1187. dev_name(port->uport_dev), dev_name(&port->dev), iw);
  1188. return rc;
  1189. }
  1190. /*
  1191. * Interleave granularity is a multiple of @parent_port granularity.
  1192. * Multiplier is the parent port interleave ways.
  1193. */
  1194. rc = granularity_to_eig(parent_ig * parent_iw, &eig);
  1195. if (rc) {
  1196. dev_dbg(&cxlr->dev,
  1197. "%s: invalid granularity calculation (%d * %d)\n",
  1198. dev_name(&parent_port->dev), parent_ig, parent_iw);
  1199. return rc;
  1200. }
  1201. rc = eig_to_granularity(eig, &ig);
  1202. if (rc) {
  1203. dev_dbg(&cxlr->dev, "%s:%s: invalid interleave: %d\n",
  1204. dev_name(port->uport_dev), dev_name(&port->dev),
  1205. 256 << eig);
  1206. return rc;
  1207. }
  1208. if (iw > 8 || iw > cxlsd->nr_targets) {
  1209. dev_dbg(&cxlr->dev,
  1210. "%s:%s:%s: ways: %d overflows targets: %d\n",
  1211. dev_name(port->uport_dev), dev_name(&port->dev),
  1212. dev_name(&cxld->dev), iw, cxlsd->nr_targets);
  1213. return -ENXIO;
  1214. }
  1215. if (test_bit(CXL_REGION_F_AUTO, &cxlr->flags)) {
  1216. if (cxld->interleave_ways != iw ||
  1217. cxld->interleave_granularity != ig ||
  1218. cxld->hpa_range.start != p->res->start ||
  1219. cxld->hpa_range.end != p->res->end ||
  1220. ((cxld->flags & CXL_DECODER_F_ENABLE) == 0)) {
  1221. dev_err(&cxlr->dev,
  1222. "%s:%s %s expected iw: %d ig: %d %pr\n",
  1223. dev_name(port->uport_dev), dev_name(&port->dev),
  1224. __func__, iw, ig, p->res);
  1225. dev_err(&cxlr->dev,
  1226. "%s:%s %s got iw: %d ig: %d state: %s %#llx:%#llx\n",
  1227. dev_name(port->uport_dev), dev_name(&port->dev),
  1228. __func__, cxld->interleave_ways,
  1229. cxld->interleave_granularity,
  1230. (cxld->flags & CXL_DECODER_F_ENABLE) ?
  1231. "enabled" :
  1232. "disabled",
  1233. cxld->hpa_range.start, cxld->hpa_range.end);
  1234. return -ENXIO;
  1235. }
  1236. } else {
  1237. rc = check_interleave_cap(cxld, iw, ig);
  1238. if (rc) {
  1239. dev_dbg(&cxlr->dev,
  1240. "%s:%s iw: %d ig: %d is not supported\n",
  1241. dev_name(port->uport_dev),
  1242. dev_name(&port->dev), iw, ig);
  1243. return rc;
  1244. }
  1245. cxld->interleave_ways = iw;
  1246. cxld->interleave_granularity = ig;
  1247. cxld->hpa_range = (struct range) {
  1248. .start = p->res->start,
  1249. .end = p->res->end,
  1250. };
  1251. }
  1252. dev_dbg(&cxlr->dev, "%s:%s iw: %d ig: %d\n", dev_name(port->uport_dev),
  1253. dev_name(&port->dev), iw, ig);
  1254. add_target:
  1255. if (cxl_rr->nr_targets_set == cxl_rr->nr_targets) {
  1256. dev_dbg(&cxlr->dev,
  1257. "%s:%s: targets full trying to add %s:%s at %d\n",
  1258. dev_name(port->uport_dev), dev_name(&port->dev),
  1259. dev_name(&cxlmd->dev), dev_name(&cxled->cxld.dev), pos);
  1260. return -ENXIO;
  1261. }
  1262. if (test_bit(CXL_REGION_F_AUTO, &cxlr->flags)) {
  1263. if (cxlsd->target[cxl_rr->nr_targets_set] != ep->dport) {
  1264. dev_dbg(&cxlr->dev, "%s:%s: %s expected %s at %d\n",
  1265. dev_name(port->uport_dev), dev_name(&port->dev),
  1266. dev_name(&cxlsd->cxld.dev),
  1267. dev_name(ep->dport->dport_dev),
  1268. cxl_rr->nr_targets_set);
  1269. return -ENXIO;
  1270. }
  1271. } else
  1272. cxlsd->target[cxl_rr->nr_targets_set] = ep->dport;
  1273. inc = 1;
  1274. out_target_set:
  1275. cxl_rr->nr_targets_set += inc;
  1276. dev_dbg(&cxlr->dev, "%s:%s target[%d] = %s for %s:%s @ %d\n",
  1277. dev_name(port->uport_dev), dev_name(&port->dev),
  1278. cxl_rr->nr_targets_set - 1, dev_name(ep->dport->dport_dev),
  1279. dev_name(&cxlmd->dev), dev_name(&cxled->cxld.dev), pos);
  1280. return 0;
  1281. }
  1282. static void cxl_port_reset_targets(struct cxl_port *port,
  1283. struct cxl_region *cxlr)
  1284. {
  1285. struct cxl_region_ref *cxl_rr = cxl_rr_load(port, cxlr);
  1286. struct cxl_decoder *cxld;
  1287. /*
  1288. * After the last endpoint has been detached the entire cxl_rr may now
  1289. * be gone.
  1290. */
  1291. if (!cxl_rr)
  1292. return;
  1293. cxl_rr->nr_targets_set = 0;
  1294. cxld = cxl_rr->decoder;
  1295. cxld->hpa_range = (struct range) {
  1296. .start = 0,
  1297. .end = -1,
  1298. };
  1299. }
  1300. static void cxl_region_teardown_targets(struct cxl_region *cxlr)
  1301. {
  1302. struct cxl_region_params *p = &cxlr->params;
  1303. struct cxl_endpoint_decoder *cxled;
  1304. struct cxl_dev_state *cxlds;
  1305. struct cxl_memdev *cxlmd;
  1306. struct cxl_port *iter;
  1307. struct cxl_ep *ep;
  1308. int i;
  1309. /*
  1310. * In the auto-discovery case skip automatic teardown since the
  1311. * address space is already active
  1312. */
  1313. if (test_bit(CXL_REGION_F_AUTO, &cxlr->flags))
  1314. return;
  1315. for (i = 0; i < p->nr_targets; i++) {
  1316. cxled = p->targets[i];
  1317. cxlmd = cxled_to_memdev(cxled);
  1318. cxlds = cxlmd->cxlds;
  1319. if (cxlds->rcd)
  1320. continue;
  1321. iter = cxled_to_port(cxled);
  1322. while (!is_cxl_root(to_cxl_port(iter->dev.parent)))
  1323. iter = to_cxl_port(iter->dev.parent);
  1324. for (ep = cxl_ep_load(iter, cxlmd); iter;
  1325. iter = ep->next, ep = cxl_ep_load(iter, cxlmd))
  1326. cxl_port_reset_targets(iter, cxlr);
  1327. }
  1328. }
  1329. static int cxl_region_setup_targets(struct cxl_region *cxlr)
  1330. {
  1331. struct cxl_region_params *p = &cxlr->params;
  1332. struct cxl_endpoint_decoder *cxled;
  1333. struct cxl_dev_state *cxlds;
  1334. int i, rc, rch = 0, vh = 0;
  1335. struct cxl_memdev *cxlmd;
  1336. struct cxl_port *iter;
  1337. struct cxl_ep *ep;
  1338. for (i = 0; i < p->nr_targets; i++) {
  1339. cxled = p->targets[i];
  1340. cxlmd = cxled_to_memdev(cxled);
  1341. cxlds = cxlmd->cxlds;
  1342. /* validate that all targets agree on topology */
  1343. if (!cxlds->rcd) {
  1344. vh++;
  1345. } else {
  1346. rch++;
  1347. continue;
  1348. }
  1349. iter = cxled_to_port(cxled);
  1350. while (!is_cxl_root(to_cxl_port(iter->dev.parent)))
  1351. iter = to_cxl_port(iter->dev.parent);
  1352. /*
  1353. * Descend the topology tree programming / validating
  1354. * targets while looking for conflicts.
  1355. */
  1356. for (ep = cxl_ep_load(iter, cxlmd); iter;
  1357. iter = ep->next, ep = cxl_ep_load(iter, cxlmd)) {
  1358. rc = cxl_port_setup_targets(iter, cxlr, cxled);
  1359. if (rc) {
  1360. cxl_region_teardown_targets(cxlr);
  1361. return rc;
  1362. }
  1363. }
  1364. }
  1365. if (rch && vh) {
  1366. dev_err(&cxlr->dev, "mismatched CXL topologies detected\n");
  1367. cxl_region_teardown_targets(cxlr);
  1368. return -ENXIO;
  1369. }
  1370. return 0;
  1371. }
  1372. static int cxl_region_validate_position(struct cxl_region *cxlr,
  1373. struct cxl_endpoint_decoder *cxled,
  1374. int pos)
  1375. {
  1376. struct cxl_memdev *cxlmd = cxled_to_memdev(cxled);
  1377. struct cxl_region_params *p = &cxlr->params;
  1378. int i;
  1379. if (pos < 0 || pos >= p->interleave_ways) {
  1380. dev_dbg(&cxlr->dev, "position %d out of range %d\n", pos,
  1381. p->interleave_ways);
  1382. return -ENXIO;
  1383. }
  1384. if (p->targets[pos] == cxled)
  1385. return 0;
  1386. if (p->targets[pos]) {
  1387. struct cxl_endpoint_decoder *cxled_target = p->targets[pos];
  1388. struct cxl_memdev *cxlmd_target = cxled_to_memdev(cxled_target);
  1389. dev_dbg(&cxlr->dev, "position %d already assigned to %s:%s\n",
  1390. pos, dev_name(&cxlmd_target->dev),
  1391. dev_name(&cxled_target->cxld.dev));
  1392. return -EBUSY;
  1393. }
  1394. for (i = 0; i < p->interleave_ways; i++) {
  1395. struct cxl_endpoint_decoder *cxled_target;
  1396. struct cxl_memdev *cxlmd_target;
  1397. cxled_target = p->targets[i];
  1398. if (!cxled_target)
  1399. continue;
  1400. cxlmd_target = cxled_to_memdev(cxled_target);
  1401. if (cxlmd_target == cxlmd) {
  1402. dev_dbg(&cxlr->dev,
  1403. "%s already specified at position %d via: %s\n",
  1404. dev_name(&cxlmd->dev), pos,
  1405. dev_name(&cxled_target->cxld.dev));
  1406. return -EBUSY;
  1407. }
  1408. }
  1409. return 0;
  1410. }
  1411. static int cxl_region_attach_position(struct cxl_region *cxlr,
  1412. struct cxl_root_decoder *cxlrd,
  1413. struct cxl_endpoint_decoder *cxled,
  1414. const struct cxl_dport *dport, int pos)
  1415. {
  1416. struct cxl_memdev *cxlmd = cxled_to_memdev(cxled);
  1417. struct cxl_switch_decoder *cxlsd = &cxlrd->cxlsd;
  1418. struct cxl_decoder *cxld = &cxlsd->cxld;
  1419. int iw = cxld->interleave_ways;
  1420. struct cxl_port *iter;
  1421. int rc;
  1422. if (dport != cxlrd->cxlsd.target[pos % iw]) {
  1423. dev_dbg(&cxlr->dev, "%s:%s invalid target position for %s\n",
  1424. dev_name(&cxlmd->dev), dev_name(&cxled->cxld.dev),
  1425. dev_name(&cxlrd->cxlsd.cxld.dev));
  1426. return -ENXIO;
  1427. }
  1428. for (iter = cxled_to_port(cxled); !is_cxl_root(iter);
  1429. iter = to_cxl_port(iter->dev.parent)) {
  1430. rc = cxl_port_attach_region(iter, cxlr, cxled, pos);
  1431. if (rc)
  1432. goto err;
  1433. }
  1434. return 0;
  1435. err:
  1436. for (iter = cxled_to_port(cxled); !is_cxl_root(iter);
  1437. iter = to_cxl_port(iter->dev.parent))
  1438. cxl_port_detach_region(iter, cxlr, cxled);
  1439. return rc;
  1440. }
  1441. static int cxl_region_attach_auto(struct cxl_region *cxlr,
  1442. struct cxl_endpoint_decoder *cxled, int pos)
  1443. {
  1444. struct cxl_region_params *p = &cxlr->params;
  1445. if (cxled->state != CXL_DECODER_STATE_AUTO) {
  1446. dev_err(&cxlr->dev,
  1447. "%s: unable to add decoder to autodetected region\n",
  1448. dev_name(&cxled->cxld.dev));
  1449. return -EINVAL;
  1450. }
  1451. if (pos >= 0) {
  1452. dev_dbg(&cxlr->dev, "%s: expected auto position, not %d\n",
  1453. dev_name(&cxled->cxld.dev), pos);
  1454. return -EINVAL;
  1455. }
  1456. if (p->nr_targets >= p->interleave_ways) {
  1457. dev_err(&cxlr->dev, "%s: no more target slots available\n",
  1458. dev_name(&cxled->cxld.dev));
  1459. return -ENXIO;
  1460. }
  1461. /*
  1462. * Temporarily record the endpoint decoder into the target array. Yes,
  1463. * this means that userspace can view devices in the wrong position
  1464. * before the region activates, and must be careful to understand when
  1465. * it might be racing region autodiscovery.
  1466. */
  1467. pos = p->nr_targets;
  1468. p->targets[pos] = cxled;
  1469. cxled->pos = pos;
  1470. p->nr_targets++;
  1471. return 0;
  1472. }
  1473. static int cmp_interleave_pos(const void *a, const void *b)
  1474. {
  1475. struct cxl_endpoint_decoder *cxled_a = *(typeof(cxled_a) *)a;
  1476. struct cxl_endpoint_decoder *cxled_b = *(typeof(cxled_b) *)b;
  1477. return cxled_a->pos - cxled_b->pos;
  1478. }
  1479. static struct cxl_port *next_port(struct cxl_port *port)
  1480. {
  1481. if (!port->parent_dport)
  1482. return NULL;
  1483. return port->parent_dport->port;
  1484. }
  1485. static int match_switch_decoder_by_range(struct device *dev, void *data)
  1486. {
  1487. struct cxl_switch_decoder *cxlsd;
  1488. struct range *r1, *r2 = data;
  1489. if (!is_switch_decoder(dev))
  1490. return 0;
  1491. cxlsd = to_cxl_switch_decoder(dev);
  1492. r1 = &cxlsd->cxld.hpa_range;
  1493. if (is_root_decoder(dev))
  1494. return range_contains(r1, r2);
  1495. return (r1->start == r2->start && r1->end == r2->end);
  1496. }
  1497. static int find_pos_and_ways(struct cxl_port *port, struct range *range,
  1498. int *pos, int *ways)
  1499. {
  1500. struct cxl_switch_decoder *cxlsd;
  1501. struct cxl_port *parent;
  1502. struct device *dev;
  1503. int rc = -ENXIO;
  1504. parent = next_port(port);
  1505. if (!parent)
  1506. return rc;
  1507. dev = device_find_child(&parent->dev, range,
  1508. match_switch_decoder_by_range);
  1509. if (!dev) {
  1510. dev_err(port->uport_dev,
  1511. "failed to find decoder mapping %#llx-%#llx\n",
  1512. range->start, range->end);
  1513. return rc;
  1514. }
  1515. cxlsd = to_cxl_switch_decoder(dev);
  1516. *ways = cxlsd->cxld.interleave_ways;
  1517. for (int i = 0; i < *ways; i++) {
  1518. if (cxlsd->target[i] == port->parent_dport) {
  1519. *pos = i;
  1520. rc = 0;
  1521. break;
  1522. }
  1523. }
  1524. put_device(dev);
  1525. if (rc)
  1526. dev_err(port->uport_dev,
  1527. "failed to find %s:%s in target list of %s\n",
  1528. dev_name(&port->dev),
  1529. dev_name(port->parent_dport->dport_dev),
  1530. dev_name(&cxlsd->cxld.dev));
  1531. return rc;
  1532. }
  1533. /**
  1534. * cxl_calc_interleave_pos() - calculate an endpoint position in a region
  1535. * @cxled: endpoint decoder member of given region
  1536. *
  1537. * The endpoint position is calculated by traversing the topology from
  1538. * the endpoint to the root decoder and iteratively applying this
  1539. * calculation:
  1540. *
  1541. * position = position * parent_ways + parent_pos;
  1542. *
  1543. * ...where @position is inferred from switch and root decoder target lists.
  1544. *
  1545. * Return: position >= 0 on success
  1546. * -ENXIO on failure
  1547. */
  1548. static int cxl_calc_interleave_pos(struct cxl_endpoint_decoder *cxled)
  1549. {
  1550. struct cxl_port *iter, *port = cxled_to_port(cxled);
  1551. struct cxl_memdev *cxlmd = cxled_to_memdev(cxled);
  1552. struct range *range = &cxled->cxld.hpa_range;
  1553. int parent_ways = 0, parent_pos = 0, pos = 0;
  1554. int rc;
  1555. /*
  1556. * Example: the expected interleave order of the 4-way region shown
  1557. * below is: mem0, mem2, mem1, mem3
  1558. *
  1559. * root_port
  1560. * / \
  1561. * host_bridge_0 host_bridge_1
  1562. * | | | |
  1563. * mem0 mem1 mem2 mem3
  1564. *
  1565. * In the example the calculator will iterate twice. The first iteration
  1566. * uses the mem position in the host-bridge and the ways of the host-
  1567. * bridge to generate the first, or local, position. The second
  1568. * iteration uses the host-bridge position in the root_port and the ways
  1569. * of the root_port to refine the position.
  1570. *
  1571. * A trace of the calculation per endpoint looks like this:
  1572. * mem0: pos = 0 * 2 + 0 mem2: pos = 0 * 2 + 0
  1573. * pos = 0 * 2 + 0 pos = 0 * 2 + 1
  1574. * pos: 0 pos: 1
  1575. *
  1576. * mem1: pos = 0 * 2 + 1 mem3: pos = 0 * 2 + 1
  1577. * pos = 1 * 2 + 0 pos = 1 * 2 + 1
  1578. * pos: 2 pos = 3
  1579. *
  1580. * Note that while this example is simple, the method applies to more
  1581. * complex topologies, including those with switches.
  1582. */
  1583. /* Iterate from endpoint to root_port refining the position */
  1584. for (iter = port; iter; iter = next_port(iter)) {
  1585. if (is_cxl_root(iter))
  1586. break;
  1587. rc = find_pos_and_ways(iter, range, &parent_pos, &parent_ways);
  1588. if (rc)
  1589. return rc;
  1590. pos = pos * parent_ways + parent_pos;
  1591. }
  1592. dev_dbg(&cxlmd->dev,
  1593. "decoder:%s parent:%s port:%s range:%#llx-%#llx pos:%d\n",
  1594. dev_name(&cxled->cxld.dev), dev_name(cxlmd->dev.parent),
  1595. dev_name(&port->dev), range->start, range->end, pos);
  1596. return pos;
  1597. }
  1598. static int cxl_region_sort_targets(struct cxl_region *cxlr)
  1599. {
  1600. struct cxl_region_params *p = &cxlr->params;
  1601. int i, rc = 0;
  1602. for (i = 0; i < p->nr_targets; i++) {
  1603. struct cxl_endpoint_decoder *cxled = p->targets[i];
  1604. cxled->pos = cxl_calc_interleave_pos(cxled);
  1605. /*
  1606. * Record that sorting failed, but still continue to calc
  1607. * cxled->pos so that follow-on code paths can reliably
  1608. * do p->targets[cxled->pos] to self-reference their entry.
  1609. */
  1610. if (cxled->pos < 0)
  1611. rc = -ENXIO;
  1612. }
  1613. /* Keep the cxlr target list in interleave position order */
  1614. sort(p->targets, p->nr_targets, sizeof(p->targets[0]),
  1615. cmp_interleave_pos, NULL);
  1616. dev_dbg(&cxlr->dev, "region sort %s\n", rc ? "failed" : "successful");
  1617. return rc;
  1618. }
  1619. static int cxl_region_attach(struct cxl_region *cxlr,
  1620. struct cxl_endpoint_decoder *cxled, int pos)
  1621. {
  1622. struct cxl_root_decoder *cxlrd = to_cxl_root_decoder(cxlr->dev.parent);
  1623. struct cxl_memdev *cxlmd = cxled_to_memdev(cxled);
  1624. struct cxl_region_params *p = &cxlr->params;
  1625. struct cxl_port *ep_port, *root_port;
  1626. struct cxl_dport *dport;
  1627. int rc = -ENXIO;
  1628. rc = check_interleave_cap(&cxled->cxld, p->interleave_ways,
  1629. p->interleave_granularity);
  1630. if (rc) {
  1631. dev_dbg(&cxlr->dev, "%s iw: %d ig: %d is not supported\n",
  1632. dev_name(&cxled->cxld.dev), p->interleave_ways,
  1633. p->interleave_granularity);
  1634. return rc;
  1635. }
  1636. if (cxled->mode != cxlr->mode) {
  1637. dev_dbg(&cxlr->dev, "%s region mode: %d mismatch: %d\n",
  1638. dev_name(&cxled->cxld.dev), cxlr->mode, cxled->mode);
  1639. return -EINVAL;
  1640. }
  1641. if (cxled->mode == CXL_DECODER_DEAD) {
  1642. dev_dbg(&cxlr->dev, "%s dead\n", dev_name(&cxled->cxld.dev));
  1643. return -ENODEV;
  1644. }
  1645. /* all full of members, or interleave config not established? */
  1646. if (p->state > CXL_CONFIG_INTERLEAVE_ACTIVE) {
  1647. dev_dbg(&cxlr->dev, "region already active\n");
  1648. return -EBUSY;
  1649. } else if (p->state < CXL_CONFIG_INTERLEAVE_ACTIVE) {
  1650. dev_dbg(&cxlr->dev, "interleave config missing\n");
  1651. return -ENXIO;
  1652. }
  1653. if (p->nr_targets >= p->interleave_ways) {
  1654. dev_dbg(&cxlr->dev, "region already has %d endpoints\n",
  1655. p->nr_targets);
  1656. return -EINVAL;
  1657. }
  1658. ep_port = cxled_to_port(cxled);
  1659. root_port = cxlrd_to_port(cxlrd);
  1660. dport = cxl_find_dport_by_dev(root_port, ep_port->host_bridge);
  1661. if (!dport) {
  1662. dev_dbg(&cxlr->dev, "%s:%s invalid target for %s\n",
  1663. dev_name(&cxlmd->dev), dev_name(&cxled->cxld.dev),
  1664. dev_name(cxlr->dev.parent));
  1665. return -ENXIO;
  1666. }
  1667. if (cxled->cxld.target_type != cxlr->type) {
  1668. dev_dbg(&cxlr->dev, "%s:%s type mismatch: %d vs %d\n",
  1669. dev_name(&cxlmd->dev), dev_name(&cxled->cxld.dev),
  1670. cxled->cxld.target_type, cxlr->type);
  1671. return -ENXIO;
  1672. }
  1673. if (!cxled->dpa_res) {
  1674. dev_dbg(&cxlr->dev, "%s:%s: missing DPA allocation.\n",
  1675. dev_name(&cxlmd->dev), dev_name(&cxled->cxld.dev));
  1676. return -ENXIO;
  1677. }
  1678. if (resource_size(cxled->dpa_res) * p->interleave_ways !=
  1679. resource_size(p->res)) {
  1680. dev_dbg(&cxlr->dev,
  1681. "%s:%s: decoder-size-%#llx * ways-%d != region-size-%#llx\n",
  1682. dev_name(&cxlmd->dev), dev_name(&cxled->cxld.dev),
  1683. (u64)resource_size(cxled->dpa_res), p->interleave_ways,
  1684. (u64)resource_size(p->res));
  1685. return -EINVAL;
  1686. }
  1687. cxl_region_perf_data_calculate(cxlr, cxled);
  1688. if (test_bit(CXL_REGION_F_AUTO, &cxlr->flags)) {
  1689. int i;
  1690. rc = cxl_region_attach_auto(cxlr, cxled, pos);
  1691. if (rc)
  1692. return rc;
  1693. /* await more targets to arrive... */
  1694. if (p->nr_targets < p->interleave_ways)
  1695. return 0;
  1696. /*
  1697. * All targets are here, which implies all PCI enumeration that
  1698. * affects this region has been completed. Walk the topology to
  1699. * sort the devices into their relative region decode position.
  1700. */
  1701. rc = cxl_region_sort_targets(cxlr);
  1702. if (rc)
  1703. return rc;
  1704. for (i = 0; i < p->nr_targets; i++) {
  1705. cxled = p->targets[i];
  1706. ep_port = cxled_to_port(cxled);
  1707. dport = cxl_find_dport_by_dev(root_port,
  1708. ep_port->host_bridge);
  1709. rc = cxl_region_attach_position(cxlr, cxlrd, cxled,
  1710. dport, i);
  1711. if (rc)
  1712. return rc;
  1713. }
  1714. rc = cxl_region_setup_targets(cxlr);
  1715. if (rc)
  1716. return rc;
  1717. /*
  1718. * If target setup succeeds in the autodiscovery case
  1719. * then the region is already committed.
  1720. */
  1721. p->state = CXL_CONFIG_COMMIT;
  1722. cxl_region_shared_upstream_bandwidth_update(cxlr);
  1723. return 0;
  1724. }
  1725. rc = cxl_region_validate_position(cxlr, cxled, pos);
  1726. if (rc)
  1727. return rc;
  1728. rc = cxl_region_attach_position(cxlr, cxlrd, cxled, dport, pos);
  1729. if (rc)
  1730. return rc;
  1731. p->targets[pos] = cxled;
  1732. cxled->pos = pos;
  1733. p->nr_targets++;
  1734. if (p->nr_targets == p->interleave_ways) {
  1735. rc = cxl_region_setup_targets(cxlr);
  1736. if (rc)
  1737. return rc;
  1738. p->state = CXL_CONFIG_ACTIVE;
  1739. cxl_region_shared_upstream_bandwidth_update(cxlr);
  1740. }
  1741. cxled->cxld.interleave_ways = p->interleave_ways;
  1742. cxled->cxld.interleave_granularity = p->interleave_granularity;
  1743. cxled->cxld.hpa_range = (struct range) {
  1744. .start = p->res->start,
  1745. .end = p->res->end,
  1746. };
  1747. if (p->nr_targets != p->interleave_ways)
  1748. return 0;
  1749. /*
  1750. * Test the auto-discovery position calculator function
  1751. * against this successfully created user-defined region.
  1752. * A fail message here means that this interleave config
  1753. * will fail when presented as CXL_REGION_F_AUTO.
  1754. */
  1755. for (int i = 0; i < p->nr_targets; i++) {
  1756. struct cxl_endpoint_decoder *cxled = p->targets[i];
  1757. int test_pos;
  1758. test_pos = cxl_calc_interleave_pos(cxled);
  1759. dev_dbg(&cxled->cxld.dev,
  1760. "Test cxl_calc_interleave_pos(): %s test_pos:%d cxled->pos:%d\n",
  1761. (test_pos == cxled->pos) ? "success" : "fail",
  1762. test_pos, cxled->pos);
  1763. }
  1764. return 0;
  1765. }
  1766. static int cxl_region_detach(struct cxl_endpoint_decoder *cxled)
  1767. {
  1768. struct cxl_port *iter, *ep_port = cxled_to_port(cxled);
  1769. struct cxl_region *cxlr = cxled->cxld.region;
  1770. struct cxl_region_params *p;
  1771. int rc = 0;
  1772. lockdep_assert_held_write(&cxl_region_rwsem);
  1773. if (!cxlr)
  1774. return 0;
  1775. p = &cxlr->params;
  1776. get_device(&cxlr->dev);
  1777. if (p->state > CXL_CONFIG_ACTIVE) {
  1778. cxl_region_decode_reset(cxlr, p->interleave_ways);
  1779. p->state = CXL_CONFIG_ACTIVE;
  1780. }
  1781. for (iter = ep_port; !is_cxl_root(iter);
  1782. iter = to_cxl_port(iter->dev.parent))
  1783. cxl_port_detach_region(iter, cxlr, cxled);
  1784. if (cxled->pos < 0 || cxled->pos >= p->interleave_ways ||
  1785. p->targets[cxled->pos] != cxled) {
  1786. struct cxl_memdev *cxlmd = cxled_to_memdev(cxled);
  1787. dev_WARN_ONCE(&cxlr->dev, 1, "expected %s:%s at position %d\n",
  1788. dev_name(&cxlmd->dev), dev_name(&cxled->cxld.dev),
  1789. cxled->pos);
  1790. goto out;
  1791. }
  1792. if (p->state == CXL_CONFIG_ACTIVE) {
  1793. p->state = CXL_CONFIG_INTERLEAVE_ACTIVE;
  1794. cxl_region_teardown_targets(cxlr);
  1795. }
  1796. p->targets[cxled->pos] = NULL;
  1797. p->nr_targets--;
  1798. cxled->cxld.hpa_range = (struct range) {
  1799. .start = 0,
  1800. .end = -1,
  1801. };
  1802. /* notify the region driver that one of its targets has departed */
  1803. up_write(&cxl_region_rwsem);
  1804. device_release_driver(&cxlr->dev);
  1805. down_write(&cxl_region_rwsem);
  1806. out:
  1807. put_device(&cxlr->dev);
  1808. return rc;
  1809. }
  1810. void cxl_decoder_kill_region(struct cxl_endpoint_decoder *cxled)
  1811. {
  1812. down_write(&cxl_region_rwsem);
  1813. cxled->mode = CXL_DECODER_DEAD;
  1814. cxl_region_detach(cxled);
  1815. up_write(&cxl_region_rwsem);
  1816. }
  1817. static int attach_target(struct cxl_region *cxlr,
  1818. struct cxl_endpoint_decoder *cxled, int pos,
  1819. unsigned int state)
  1820. {
  1821. int rc = 0;
  1822. if (state == TASK_INTERRUPTIBLE)
  1823. rc = down_write_killable(&cxl_region_rwsem);
  1824. else
  1825. down_write(&cxl_region_rwsem);
  1826. if (rc)
  1827. return rc;
  1828. down_read(&cxl_dpa_rwsem);
  1829. rc = cxl_region_attach(cxlr, cxled, pos);
  1830. up_read(&cxl_dpa_rwsem);
  1831. up_write(&cxl_region_rwsem);
  1832. return rc;
  1833. }
  1834. static int detach_target(struct cxl_region *cxlr, int pos)
  1835. {
  1836. struct cxl_region_params *p = &cxlr->params;
  1837. int rc;
  1838. rc = down_write_killable(&cxl_region_rwsem);
  1839. if (rc)
  1840. return rc;
  1841. if (pos >= p->interleave_ways) {
  1842. dev_dbg(&cxlr->dev, "position %d out of range %d\n", pos,
  1843. p->interleave_ways);
  1844. rc = -ENXIO;
  1845. goto out;
  1846. }
  1847. if (!p->targets[pos]) {
  1848. rc = 0;
  1849. goto out;
  1850. }
  1851. rc = cxl_region_detach(p->targets[pos]);
  1852. out:
  1853. up_write(&cxl_region_rwsem);
  1854. return rc;
  1855. }
  1856. static size_t store_targetN(struct cxl_region *cxlr, const char *buf, int pos,
  1857. size_t len)
  1858. {
  1859. int rc;
  1860. if (sysfs_streq(buf, "\n"))
  1861. rc = detach_target(cxlr, pos);
  1862. else {
  1863. struct device *dev;
  1864. dev = bus_find_device_by_name(&cxl_bus_type, NULL, buf);
  1865. if (!dev)
  1866. return -ENODEV;
  1867. if (!is_endpoint_decoder(dev)) {
  1868. rc = -EINVAL;
  1869. goto out;
  1870. }
  1871. rc = attach_target(cxlr, to_cxl_endpoint_decoder(dev), pos,
  1872. TASK_INTERRUPTIBLE);
  1873. out:
  1874. put_device(dev);
  1875. }
  1876. if (rc < 0)
  1877. return rc;
  1878. return len;
  1879. }
  1880. #define TARGET_ATTR_RW(n) \
  1881. static ssize_t target##n##_show( \
  1882. struct device *dev, struct device_attribute *attr, char *buf) \
  1883. { \
  1884. return show_targetN(to_cxl_region(dev), buf, (n)); \
  1885. } \
  1886. static ssize_t target##n##_store(struct device *dev, \
  1887. struct device_attribute *attr, \
  1888. const char *buf, size_t len) \
  1889. { \
  1890. return store_targetN(to_cxl_region(dev), buf, (n), len); \
  1891. } \
  1892. static DEVICE_ATTR_RW(target##n)
  1893. TARGET_ATTR_RW(0);
  1894. TARGET_ATTR_RW(1);
  1895. TARGET_ATTR_RW(2);
  1896. TARGET_ATTR_RW(3);
  1897. TARGET_ATTR_RW(4);
  1898. TARGET_ATTR_RW(5);
  1899. TARGET_ATTR_RW(6);
  1900. TARGET_ATTR_RW(7);
  1901. TARGET_ATTR_RW(8);
  1902. TARGET_ATTR_RW(9);
  1903. TARGET_ATTR_RW(10);
  1904. TARGET_ATTR_RW(11);
  1905. TARGET_ATTR_RW(12);
  1906. TARGET_ATTR_RW(13);
  1907. TARGET_ATTR_RW(14);
  1908. TARGET_ATTR_RW(15);
  1909. static struct attribute *target_attrs[] = {
  1910. &dev_attr_target0.attr,
  1911. &dev_attr_target1.attr,
  1912. &dev_attr_target2.attr,
  1913. &dev_attr_target3.attr,
  1914. &dev_attr_target4.attr,
  1915. &dev_attr_target5.attr,
  1916. &dev_attr_target6.attr,
  1917. &dev_attr_target7.attr,
  1918. &dev_attr_target8.attr,
  1919. &dev_attr_target9.attr,
  1920. &dev_attr_target10.attr,
  1921. &dev_attr_target11.attr,
  1922. &dev_attr_target12.attr,
  1923. &dev_attr_target13.attr,
  1924. &dev_attr_target14.attr,
  1925. &dev_attr_target15.attr,
  1926. NULL,
  1927. };
  1928. static umode_t cxl_region_target_visible(struct kobject *kobj,
  1929. struct attribute *a, int n)
  1930. {
  1931. struct device *dev = kobj_to_dev(kobj);
  1932. struct cxl_region *cxlr = to_cxl_region(dev);
  1933. struct cxl_region_params *p = &cxlr->params;
  1934. if (n < p->interleave_ways)
  1935. return a->mode;
  1936. return 0;
  1937. }
  1938. static const struct attribute_group cxl_region_target_group = {
  1939. .attrs = target_attrs,
  1940. .is_visible = cxl_region_target_visible,
  1941. };
  1942. static const struct attribute_group *get_cxl_region_target_group(void)
  1943. {
  1944. return &cxl_region_target_group;
  1945. }
  1946. static const struct attribute_group *region_groups[] = {
  1947. &cxl_base_attribute_group,
  1948. &cxl_region_group,
  1949. &cxl_region_target_group,
  1950. &cxl_region_access0_coordinate_group,
  1951. &cxl_region_access1_coordinate_group,
  1952. NULL,
  1953. };
  1954. static void cxl_region_release(struct device *dev)
  1955. {
  1956. struct cxl_root_decoder *cxlrd = to_cxl_root_decoder(dev->parent);
  1957. struct cxl_region *cxlr = to_cxl_region(dev);
  1958. int id = atomic_read(&cxlrd->region_id);
  1959. /*
  1960. * Try to reuse the recently idled id rather than the cached
  1961. * next id to prevent the region id space from increasing
  1962. * unnecessarily.
  1963. */
  1964. if (cxlr->id < id)
  1965. if (atomic_try_cmpxchg(&cxlrd->region_id, &id, cxlr->id)) {
  1966. memregion_free(id);
  1967. goto out;
  1968. }
  1969. memregion_free(cxlr->id);
  1970. out:
  1971. put_device(dev->parent);
  1972. kfree(cxlr);
  1973. }
  1974. const struct device_type cxl_region_type = {
  1975. .name = "cxl_region",
  1976. .release = cxl_region_release,
  1977. .groups = region_groups
  1978. };
  1979. bool is_cxl_region(struct device *dev)
  1980. {
  1981. return dev->type == &cxl_region_type;
  1982. }
  1983. EXPORT_SYMBOL_NS_GPL(is_cxl_region, CXL);
  1984. static struct cxl_region *to_cxl_region(struct device *dev)
  1985. {
  1986. if (dev_WARN_ONCE(dev, dev->type != &cxl_region_type,
  1987. "not a cxl_region device\n"))
  1988. return NULL;
  1989. return container_of(dev, struct cxl_region, dev);
  1990. }
  1991. static void unregister_region(void *_cxlr)
  1992. {
  1993. struct cxl_region *cxlr = _cxlr;
  1994. struct cxl_region_params *p = &cxlr->params;
  1995. int i;
  1996. device_del(&cxlr->dev);
  1997. /*
  1998. * Now that region sysfs is shutdown, the parameter block is now
  1999. * read-only, so no need to hold the region rwsem to access the
  2000. * region parameters.
  2001. */
  2002. for (i = 0; i < p->interleave_ways; i++)
  2003. detach_target(cxlr, i);
  2004. cxl_region_iomem_release(cxlr);
  2005. put_device(&cxlr->dev);
  2006. }
  2007. static struct lock_class_key cxl_region_key;
  2008. static struct cxl_region *cxl_region_alloc(struct cxl_root_decoder *cxlrd, int id)
  2009. {
  2010. struct cxl_region *cxlr;
  2011. struct device *dev;
  2012. cxlr = kzalloc(sizeof(*cxlr), GFP_KERNEL);
  2013. if (!cxlr) {
  2014. memregion_free(id);
  2015. return ERR_PTR(-ENOMEM);
  2016. }
  2017. dev = &cxlr->dev;
  2018. device_initialize(dev);
  2019. lockdep_set_class(&dev->mutex, &cxl_region_key);
  2020. dev->parent = &cxlrd->cxlsd.cxld.dev;
  2021. /*
  2022. * Keep root decoder pinned through cxl_region_release to fixup
  2023. * region id allocations
  2024. */
  2025. get_device(dev->parent);
  2026. device_set_pm_not_required(dev);
  2027. dev->bus = &cxl_bus_type;
  2028. dev->type = &cxl_region_type;
  2029. cxlr->id = id;
  2030. return cxlr;
  2031. }
  2032. static bool cxl_region_update_coordinates(struct cxl_region *cxlr, int nid)
  2033. {
  2034. int cset = 0;
  2035. int rc;
  2036. for (int i = 0; i < ACCESS_COORDINATE_MAX; i++) {
  2037. if (cxlr->coord[i].read_bandwidth) {
  2038. rc = 0;
  2039. if (cxl_need_node_perf_attrs_update(nid))
  2040. node_set_perf_attrs(nid, &cxlr->coord[i], i);
  2041. else
  2042. rc = cxl_update_hmat_access_coordinates(nid, cxlr, i);
  2043. if (rc == 0)
  2044. cset++;
  2045. }
  2046. }
  2047. if (!cset)
  2048. return false;
  2049. rc = sysfs_update_group(&cxlr->dev.kobj, get_cxl_region_access0_group());
  2050. if (rc)
  2051. dev_dbg(&cxlr->dev, "Failed to update access0 group\n");
  2052. rc = sysfs_update_group(&cxlr->dev.kobj, get_cxl_region_access1_group());
  2053. if (rc)
  2054. dev_dbg(&cxlr->dev, "Failed to update access1 group\n");
  2055. return true;
  2056. }
  2057. static int cxl_region_perf_attrs_callback(struct notifier_block *nb,
  2058. unsigned long action, void *arg)
  2059. {
  2060. struct cxl_region *cxlr = container_of(nb, struct cxl_region,
  2061. memory_notifier);
  2062. struct memory_notify *mnb = arg;
  2063. int nid = mnb->status_change_nid;
  2064. int region_nid;
  2065. if (nid == NUMA_NO_NODE || action != MEM_ONLINE)
  2066. return NOTIFY_DONE;
  2067. /*
  2068. * No need to hold cxl_region_rwsem; region parameters are stable
  2069. * within the cxl_region driver.
  2070. */
  2071. region_nid = phys_to_target_node(cxlr->params.res->start);
  2072. if (nid != region_nid)
  2073. return NOTIFY_DONE;
  2074. if (!cxl_region_update_coordinates(cxlr, nid))
  2075. return NOTIFY_DONE;
  2076. return NOTIFY_OK;
  2077. }
  2078. static int cxl_region_calculate_adistance(struct notifier_block *nb,
  2079. unsigned long nid, void *data)
  2080. {
  2081. struct cxl_region *cxlr = container_of(nb, struct cxl_region,
  2082. adist_notifier);
  2083. struct access_coordinate *perf;
  2084. int *adist = data;
  2085. int region_nid;
  2086. /*
  2087. * No need to hold cxl_region_rwsem; region parameters are stable
  2088. * within the cxl_region driver.
  2089. */
  2090. region_nid = phys_to_target_node(cxlr->params.res->start);
  2091. if (nid != region_nid)
  2092. return NOTIFY_OK;
  2093. perf = &cxlr->coord[ACCESS_COORDINATE_CPU];
  2094. if (mt_perf_to_adistance(perf, adist))
  2095. return NOTIFY_OK;
  2096. return NOTIFY_STOP;
  2097. }
  2098. /**
  2099. * devm_cxl_add_region - Adds a region to a decoder
  2100. * @cxlrd: root decoder
  2101. * @id: memregion id to create, or memregion_free() on failure
  2102. * @mode: mode for the endpoint decoders of this region
  2103. * @type: select whether this is an expander or accelerator (type-2 or type-3)
  2104. *
  2105. * This is the second step of region initialization. Regions exist within an
  2106. * address space which is mapped by a @cxlrd.
  2107. *
  2108. * Return: 0 if the region was added to the @cxlrd, else returns negative error
  2109. * code. The region will be named "regionZ" where Z is the unique region number.
  2110. */
  2111. static struct cxl_region *devm_cxl_add_region(struct cxl_root_decoder *cxlrd,
  2112. int id,
  2113. enum cxl_decoder_mode mode,
  2114. enum cxl_decoder_type type)
  2115. {
  2116. struct cxl_port *port = to_cxl_port(cxlrd->cxlsd.cxld.dev.parent);
  2117. struct cxl_region *cxlr;
  2118. struct device *dev;
  2119. int rc;
  2120. cxlr = cxl_region_alloc(cxlrd, id);
  2121. if (IS_ERR(cxlr))
  2122. return cxlr;
  2123. cxlr->mode = mode;
  2124. cxlr->type = type;
  2125. dev = &cxlr->dev;
  2126. rc = dev_set_name(dev, "region%d", id);
  2127. if (rc)
  2128. goto err;
  2129. rc = device_add(dev);
  2130. if (rc)
  2131. goto err;
  2132. rc = devm_add_action_or_reset(port->uport_dev, unregister_region, cxlr);
  2133. if (rc)
  2134. return ERR_PTR(rc);
  2135. dev_dbg(port->uport_dev, "%s: created %s\n",
  2136. dev_name(&cxlrd->cxlsd.cxld.dev), dev_name(dev));
  2137. return cxlr;
  2138. err:
  2139. put_device(dev);
  2140. return ERR_PTR(rc);
  2141. }
  2142. static ssize_t __create_region_show(struct cxl_root_decoder *cxlrd, char *buf)
  2143. {
  2144. return sysfs_emit(buf, "region%u\n", atomic_read(&cxlrd->region_id));
  2145. }
  2146. static ssize_t create_pmem_region_show(struct device *dev,
  2147. struct device_attribute *attr, char *buf)
  2148. {
  2149. return __create_region_show(to_cxl_root_decoder(dev), buf);
  2150. }
  2151. static ssize_t create_ram_region_show(struct device *dev,
  2152. struct device_attribute *attr, char *buf)
  2153. {
  2154. return __create_region_show(to_cxl_root_decoder(dev), buf);
  2155. }
  2156. static struct cxl_region *__create_region(struct cxl_root_decoder *cxlrd,
  2157. enum cxl_decoder_mode mode, int id)
  2158. {
  2159. int rc;
  2160. switch (mode) {
  2161. case CXL_DECODER_RAM:
  2162. case CXL_DECODER_PMEM:
  2163. break;
  2164. default:
  2165. dev_err(&cxlrd->cxlsd.cxld.dev, "unsupported mode %d\n", mode);
  2166. return ERR_PTR(-EINVAL);
  2167. }
  2168. rc = memregion_alloc(GFP_KERNEL);
  2169. if (rc < 0)
  2170. return ERR_PTR(rc);
  2171. if (atomic_cmpxchg(&cxlrd->region_id, id, rc) != id) {
  2172. memregion_free(rc);
  2173. return ERR_PTR(-EBUSY);
  2174. }
  2175. return devm_cxl_add_region(cxlrd, id, mode, CXL_DECODER_HOSTONLYMEM);
  2176. }
  2177. static ssize_t create_pmem_region_store(struct device *dev,
  2178. struct device_attribute *attr,
  2179. const char *buf, size_t len)
  2180. {
  2181. struct cxl_root_decoder *cxlrd = to_cxl_root_decoder(dev);
  2182. struct cxl_region *cxlr;
  2183. int rc, id;
  2184. rc = sscanf(buf, "region%d\n", &id);
  2185. if (rc != 1)
  2186. return -EINVAL;
  2187. cxlr = __create_region(cxlrd, CXL_DECODER_PMEM, id);
  2188. if (IS_ERR(cxlr))
  2189. return PTR_ERR(cxlr);
  2190. return len;
  2191. }
  2192. DEVICE_ATTR_RW(create_pmem_region);
  2193. static ssize_t create_ram_region_store(struct device *dev,
  2194. struct device_attribute *attr,
  2195. const char *buf, size_t len)
  2196. {
  2197. struct cxl_root_decoder *cxlrd = to_cxl_root_decoder(dev);
  2198. struct cxl_region *cxlr;
  2199. int rc, id;
  2200. rc = sscanf(buf, "region%d\n", &id);
  2201. if (rc != 1)
  2202. return -EINVAL;
  2203. cxlr = __create_region(cxlrd, CXL_DECODER_RAM, id);
  2204. if (IS_ERR(cxlr))
  2205. return PTR_ERR(cxlr);
  2206. return len;
  2207. }
  2208. DEVICE_ATTR_RW(create_ram_region);
  2209. static ssize_t region_show(struct device *dev, struct device_attribute *attr,
  2210. char *buf)
  2211. {
  2212. struct cxl_decoder *cxld = to_cxl_decoder(dev);
  2213. ssize_t rc;
  2214. rc = down_read_interruptible(&cxl_region_rwsem);
  2215. if (rc)
  2216. return rc;
  2217. if (cxld->region)
  2218. rc = sysfs_emit(buf, "%s\n", dev_name(&cxld->region->dev));
  2219. else
  2220. rc = sysfs_emit(buf, "\n");
  2221. up_read(&cxl_region_rwsem);
  2222. return rc;
  2223. }
  2224. DEVICE_ATTR_RO(region);
  2225. static struct cxl_region *
  2226. cxl_find_region_by_name(struct cxl_root_decoder *cxlrd, const char *name)
  2227. {
  2228. struct cxl_decoder *cxld = &cxlrd->cxlsd.cxld;
  2229. struct device *region_dev;
  2230. region_dev = device_find_child_by_name(&cxld->dev, name);
  2231. if (!region_dev)
  2232. return ERR_PTR(-ENODEV);
  2233. return to_cxl_region(region_dev);
  2234. }
  2235. static ssize_t delete_region_store(struct device *dev,
  2236. struct device_attribute *attr,
  2237. const char *buf, size_t len)
  2238. {
  2239. struct cxl_root_decoder *cxlrd = to_cxl_root_decoder(dev);
  2240. struct cxl_port *port = to_cxl_port(dev->parent);
  2241. struct cxl_region *cxlr;
  2242. cxlr = cxl_find_region_by_name(cxlrd, buf);
  2243. if (IS_ERR(cxlr))
  2244. return PTR_ERR(cxlr);
  2245. devm_release_action(port->uport_dev, unregister_region, cxlr);
  2246. put_device(&cxlr->dev);
  2247. return len;
  2248. }
  2249. DEVICE_ATTR_WO(delete_region);
  2250. static void cxl_pmem_region_release(struct device *dev)
  2251. {
  2252. struct cxl_pmem_region *cxlr_pmem = to_cxl_pmem_region(dev);
  2253. int i;
  2254. for (i = 0; i < cxlr_pmem->nr_mappings; i++) {
  2255. struct cxl_memdev *cxlmd = cxlr_pmem->mapping[i].cxlmd;
  2256. put_device(&cxlmd->dev);
  2257. }
  2258. kfree(cxlr_pmem);
  2259. }
  2260. static const struct attribute_group *cxl_pmem_region_attribute_groups[] = {
  2261. &cxl_base_attribute_group,
  2262. NULL,
  2263. };
  2264. const struct device_type cxl_pmem_region_type = {
  2265. .name = "cxl_pmem_region",
  2266. .release = cxl_pmem_region_release,
  2267. .groups = cxl_pmem_region_attribute_groups,
  2268. };
  2269. bool is_cxl_pmem_region(struct device *dev)
  2270. {
  2271. return dev->type == &cxl_pmem_region_type;
  2272. }
  2273. EXPORT_SYMBOL_NS_GPL(is_cxl_pmem_region, CXL);
  2274. struct cxl_pmem_region *to_cxl_pmem_region(struct device *dev)
  2275. {
  2276. if (dev_WARN_ONCE(dev, !is_cxl_pmem_region(dev),
  2277. "not a cxl_pmem_region device\n"))
  2278. return NULL;
  2279. return container_of(dev, struct cxl_pmem_region, dev);
  2280. }
  2281. EXPORT_SYMBOL_NS_GPL(to_cxl_pmem_region, CXL);
  2282. struct cxl_poison_context {
  2283. struct cxl_port *port;
  2284. enum cxl_decoder_mode mode;
  2285. u64 offset;
  2286. };
  2287. static int cxl_get_poison_unmapped(struct cxl_memdev *cxlmd,
  2288. struct cxl_poison_context *ctx)
  2289. {
  2290. struct cxl_dev_state *cxlds = cxlmd->cxlds;
  2291. u64 offset, length;
  2292. int rc = 0;
  2293. /*
  2294. * Collect poison for the remaining unmapped resources
  2295. * after poison is collected by committed endpoints.
  2296. *
  2297. * Knowing that PMEM must always follow RAM, get poison
  2298. * for unmapped resources based on the last decoder's mode:
  2299. * ram: scan remains of ram range, then any pmem range
  2300. * pmem: scan remains of pmem range
  2301. */
  2302. if (ctx->mode == CXL_DECODER_RAM) {
  2303. offset = ctx->offset;
  2304. length = resource_size(&cxlds->ram_res) - offset;
  2305. rc = cxl_mem_get_poison(cxlmd, offset, length, NULL);
  2306. if (rc == -EFAULT)
  2307. rc = 0;
  2308. if (rc)
  2309. return rc;
  2310. }
  2311. if (ctx->mode == CXL_DECODER_PMEM) {
  2312. offset = ctx->offset;
  2313. length = resource_size(&cxlds->dpa_res) - offset;
  2314. if (!length)
  2315. return 0;
  2316. } else if (resource_size(&cxlds->pmem_res)) {
  2317. offset = cxlds->pmem_res.start;
  2318. length = resource_size(&cxlds->pmem_res);
  2319. } else {
  2320. return 0;
  2321. }
  2322. return cxl_mem_get_poison(cxlmd, offset, length, NULL);
  2323. }
  2324. static int poison_by_decoder(struct device *dev, void *arg)
  2325. {
  2326. struct cxl_poison_context *ctx = arg;
  2327. struct cxl_endpoint_decoder *cxled;
  2328. struct cxl_memdev *cxlmd;
  2329. u64 offset, length;
  2330. int rc = 0;
  2331. if (!is_endpoint_decoder(dev))
  2332. return rc;
  2333. cxled = to_cxl_endpoint_decoder(dev);
  2334. if (!cxled->dpa_res || !resource_size(cxled->dpa_res))
  2335. return rc;
  2336. /*
  2337. * Regions are only created with single mode decoders: pmem or ram.
  2338. * Linux does not support mixed mode decoders. This means that
  2339. * reading poison per endpoint decoder adheres to the requirement
  2340. * that poison reads of pmem and ram must be separated.
  2341. * CXL 3.0 Spec 8.2.9.8.4.1
  2342. */
  2343. if (cxled->mode == CXL_DECODER_MIXED) {
  2344. dev_dbg(dev, "poison list read unsupported in mixed mode\n");
  2345. return rc;
  2346. }
  2347. cxlmd = cxled_to_memdev(cxled);
  2348. if (cxled->skip) {
  2349. offset = cxled->dpa_res->start - cxled->skip;
  2350. length = cxled->skip;
  2351. rc = cxl_mem_get_poison(cxlmd, offset, length, NULL);
  2352. if (rc == -EFAULT && cxled->mode == CXL_DECODER_RAM)
  2353. rc = 0;
  2354. if (rc)
  2355. return rc;
  2356. }
  2357. offset = cxled->dpa_res->start;
  2358. length = cxled->dpa_res->end - offset + 1;
  2359. rc = cxl_mem_get_poison(cxlmd, offset, length, cxled->cxld.region);
  2360. if (rc == -EFAULT && cxled->mode == CXL_DECODER_RAM)
  2361. rc = 0;
  2362. if (rc)
  2363. return rc;
  2364. /* Iterate until commit_end is reached */
  2365. if (cxled->cxld.id == ctx->port->commit_end) {
  2366. ctx->offset = cxled->dpa_res->end + 1;
  2367. ctx->mode = cxled->mode;
  2368. return 1;
  2369. }
  2370. return 0;
  2371. }
  2372. int cxl_get_poison_by_endpoint(struct cxl_port *port)
  2373. {
  2374. struct cxl_poison_context ctx;
  2375. int rc = 0;
  2376. ctx = (struct cxl_poison_context) {
  2377. .port = port
  2378. };
  2379. rc = device_for_each_child(&port->dev, &ctx, poison_by_decoder);
  2380. if (rc == 1)
  2381. rc = cxl_get_poison_unmapped(to_cxl_memdev(port->uport_dev),
  2382. &ctx);
  2383. return rc;
  2384. }
  2385. struct cxl_dpa_to_region_context {
  2386. struct cxl_region *cxlr;
  2387. u64 dpa;
  2388. };
  2389. static int __cxl_dpa_to_region(struct device *dev, void *arg)
  2390. {
  2391. struct cxl_dpa_to_region_context *ctx = arg;
  2392. struct cxl_endpoint_decoder *cxled;
  2393. struct cxl_region *cxlr;
  2394. u64 dpa = ctx->dpa;
  2395. if (!is_endpoint_decoder(dev))
  2396. return 0;
  2397. cxled = to_cxl_endpoint_decoder(dev);
  2398. if (!cxled || !cxled->dpa_res || !resource_size(cxled->dpa_res))
  2399. return 0;
  2400. if (dpa > cxled->dpa_res->end || dpa < cxled->dpa_res->start)
  2401. return 0;
  2402. /*
  2403. * Stop the region search (return 1) when an endpoint mapping is
  2404. * found. The region may not be fully constructed so offering
  2405. * the cxlr in the context structure is not guaranteed.
  2406. */
  2407. cxlr = cxled->cxld.region;
  2408. if (cxlr)
  2409. dev_dbg(dev, "dpa:0x%llx mapped in region:%s\n", dpa,
  2410. dev_name(&cxlr->dev));
  2411. else
  2412. dev_dbg(dev, "dpa:0x%llx mapped in endpoint:%s\n", dpa,
  2413. dev_name(dev));
  2414. ctx->cxlr = cxlr;
  2415. return 1;
  2416. }
  2417. struct cxl_region *cxl_dpa_to_region(const struct cxl_memdev *cxlmd, u64 dpa)
  2418. {
  2419. struct cxl_dpa_to_region_context ctx;
  2420. struct cxl_port *port;
  2421. ctx = (struct cxl_dpa_to_region_context) {
  2422. .dpa = dpa,
  2423. };
  2424. port = cxlmd->endpoint;
  2425. if (port && is_cxl_endpoint(port) && cxl_num_decoders_committed(port))
  2426. device_for_each_child(&port->dev, &ctx, __cxl_dpa_to_region);
  2427. return ctx.cxlr;
  2428. }
  2429. static bool cxl_is_hpa_in_chunk(u64 hpa, struct cxl_region *cxlr, int pos)
  2430. {
  2431. struct cxl_region_params *p = &cxlr->params;
  2432. int gran = p->interleave_granularity;
  2433. int ways = p->interleave_ways;
  2434. u64 offset;
  2435. /* Is the hpa in an expected chunk for its pos(-ition) */
  2436. offset = hpa - p->res->start;
  2437. offset = do_div(offset, gran * ways);
  2438. if ((offset >= pos * gran) && (offset < (pos + 1) * gran))
  2439. return true;
  2440. dev_dbg(&cxlr->dev,
  2441. "Addr trans fail: hpa 0x%llx not in expected chunk\n", hpa);
  2442. return false;
  2443. }
  2444. u64 cxl_dpa_to_hpa(struct cxl_region *cxlr, const struct cxl_memdev *cxlmd,
  2445. u64 dpa)
  2446. {
  2447. struct cxl_root_decoder *cxlrd = to_cxl_root_decoder(cxlr->dev.parent);
  2448. u64 dpa_offset, hpa_offset, bits_upper, mask_upper, hpa;
  2449. struct cxl_region_params *p = &cxlr->params;
  2450. struct cxl_endpoint_decoder *cxled = NULL;
  2451. u16 eig = 0;
  2452. u8 eiw = 0;
  2453. int pos;
  2454. for (int i = 0; i < p->nr_targets; i++) {
  2455. cxled = p->targets[i];
  2456. if (cxlmd == cxled_to_memdev(cxled))
  2457. break;
  2458. }
  2459. if (!cxled || cxlmd != cxled_to_memdev(cxled))
  2460. return ULLONG_MAX;
  2461. pos = cxled->pos;
  2462. ways_to_eiw(p->interleave_ways, &eiw);
  2463. granularity_to_eig(p->interleave_granularity, &eig);
  2464. /*
  2465. * The device position in the region interleave set was removed
  2466. * from the offset at HPA->DPA translation. To reconstruct the
  2467. * HPA, place the 'pos' in the offset.
  2468. *
  2469. * The placement of 'pos' in the HPA is determined by interleave
  2470. * ways and granularity and is defined in the CXL Spec 3.0 Section
  2471. * 8.2.4.19.13 Implementation Note: Device Decode Logic
  2472. */
  2473. /* Remove the dpa base */
  2474. dpa_offset = dpa - cxl_dpa_resource_start(cxled);
  2475. mask_upper = GENMASK_ULL(51, eig + 8);
  2476. if (eiw < 8) {
  2477. hpa_offset = (dpa_offset & mask_upper) << eiw;
  2478. hpa_offset |= pos << (eig + 8);
  2479. } else {
  2480. bits_upper = (dpa_offset & mask_upper) >> (eig + 8);
  2481. bits_upper = bits_upper * 3;
  2482. hpa_offset = ((bits_upper << (eiw - 8)) + pos) << (eig + 8);
  2483. }
  2484. /* The lower bits remain unchanged */
  2485. hpa_offset |= dpa_offset & GENMASK_ULL(eig + 7, 0);
  2486. /* Apply the hpa_offset to the region base address */
  2487. hpa = hpa_offset + p->res->start;
  2488. /* Root decoder translation overrides typical modulo decode */
  2489. if (cxlrd->hpa_to_spa)
  2490. hpa = cxlrd->hpa_to_spa(cxlrd, hpa);
  2491. if (hpa < p->res->start || hpa > p->res->end) {
  2492. dev_dbg(&cxlr->dev,
  2493. "Addr trans fail: hpa 0x%llx not in region\n", hpa);
  2494. return ULLONG_MAX;
  2495. }
  2496. /* Simple chunk check, by pos & gran, only applies to modulo decodes */
  2497. if (!cxlrd->hpa_to_spa && (!cxl_is_hpa_in_chunk(hpa, cxlr, pos)))
  2498. return ULLONG_MAX;
  2499. return hpa;
  2500. }
  2501. static struct lock_class_key cxl_pmem_region_key;
  2502. static int cxl_pmem_region_alloc(struct cxl_region *cxlr)
  2503. {
  2504. struct cxl_region_params *p = &cxlr->params;
  2505. struct cxl_nvdimm_bridge *cxl_nvb;
  2506. struct device *dev;
  2507. int i;
  2508. guard(rwsem_read)(&cxl_region_rwsem);
  2509. if (p->state != CXL_CONFIG_COMMIT)
  2510. return -ENXIO;
  2511. struct cxl_pmem_region *cxlr_pmem __free(kfree) =
  2512. kzalloc(struct_size(cxlr_pmem, mapping, p->nr_targets), GFP_KERNEL);
  2513. if (!cxlr_pmem)
  2514. return -ENOMEM;
  2515. cxlr_pmem->hpa_range.start = p->res->start;
  2516. cxlr_pmem->hpa_range.end = p->res->end;
  2517. /* Snapshot the region configuration underneath the cxl_region_rwsem */
  2518. cxlr_pmem->nr_mappings = p->nr_targets;
  2519. for (i = 0; i < p->nr_targets; i++) {
  2520. struct cxl_endpoint_decoder *cxled = p->targets[i];
  2521. struct cxl_memdev *cxlmd = cxled_to_memdev(cxled);
  2522. struct cxl_pmem_region_mapping *m = &cxlr_pmem->mapping[i];
  2523. /*
  2524. * Regions never span CXL root devices, so by definition the
  2525. * bridge for one device is the same for all.
  2526. */
  2527. if (i == 0) {
  2528. cxl_nvb = cxl_find_nvdimm_bridge(cxlmd->endpoint);
  2529. if (!cxl_nvb)
  2530. return -ENODEV;
  2531. cxlr->cxl_nvb = cxl_nvb;
  2532. }
  2533. m->cxlmd = cxlmd;
  2534. get_device(&cxlmd->dev);
  2535. m->start = cxled->dpa_res->start;
  2536. m->size = resource_size(cxled->dpa_res);
  2537. m->position = i;
  2538. }
  2539. dev = &cxlr_pmem->dev;
  2540. device_initialize(dev);
  2541. lockdep_set_class(&dev->mutex, &cxl_pmem_region_key);
  2542. device_set_pm_not_required(dev);
  2543. dev->parent = &cxlr->dev;
  2544. dev->bus = &cxl_bus_type;
  2545. dev->type = &cxl_pmem_region_type;
  2546. cxlr_pmem->cxlr = cxlr;
  2547. cxlr->cxlr_pmem = no_free_ptr(cxlr_pmem);
  2548. return 0;
  2549. }
  2550. static void cxl_dax_region_release(struct device *dev)
  2551. {
  2552. struct cxl_dax_region *cxlr_dax = to_cxl_dax_region(dev);
  2553. kfree(cxlr_dax);
  2554. }
  2555. static const struct attribute_group *cxl_dax_region_attribute_groups[] = {
  2556. &cxl_base_attribute_group,
  2557. NULL,
  2558. };
  2559. const struct device_type cxl_dax_region_type = {
  2560. .name = "cxl_dax_region",
  2561. .release = cxl_dax_region_release,
  2562. .groups = cxl_dax_region_attribute_groups,
  2563. };
  2564. static bool is_cxl_dax_region(struct device *dev)
  2565. {
  2566. return dev->type == &cxl_dax_region_type;
  2567. }
  2568. struct cxl_dax_region *to_cxl_dax_region(struct device *dev)
  2569. {
  2570. if (dev_WARN_ONCE(dev, !is_cxl_dax_region(dev),
  2571. "not a cxl_dax_region device\n"))
  2572. return NULL;
  2573. return container_of(dev, struct cxl_dax_region, dev);
  2574. }
  2575. EXPORT_SYMBOL_NS_GPL(to_cxl_dax_region, CXL);
  2576. static struct lock_class_key cxl_dax_region_key;
  2577. static struct cxl_dax_region *cxl_dax_region_alloc(struct cxl_region *cxlr)
  2578. {
  2579. struct cxl_region_params *p = &cxlr->params;
  2580. struct cxl_dax_region *cxlr_dax;
  2581. struct device *dev;
  2582. down_read(&cxl_region_rwsem);
  2583. if (p->state != CXL_CONFIG_COMMIT) {
  2584. cxlr_dax = ERR_PTR(-ENXIO);
  2585. goto out;
  2586. }
  2587. cxlr_dax = kzalloc(sizeof(*cxlr_dax), GFP_KERNEL);
  2588. if (!cxlr_dax) {
  2589. cxlr_dax = ERR_PTR(-ENOMEM);
  2590. goto out;
  2591. }
  2592. cxlr_dax->hpa_range.start = p->res->start;
  2593. cxlr_dax->hpa_range.end = p->res->end;
  2594. dev = &cxlr_dax->dev;
  2595. cxlr_dax->cxlr = cxlr;
  2596. device_initialize(dev);
  2597. lockdep_set_class(&dev->mutex, &cxl_dax_region_key);
  2598. device_set_pm_not_required(dev);
  2599. dev->parent = &cxlr->dev;
  2600. dev->bus = &cxl_bus_type;
  2601. dev->type = &cxl_dax_region_type;
  2602. out:
  2603. up_read(&cxl_region_rwsem);
  2604. return cxlr_dax;
  2605. }
  2606. static void cxlr_pmem_unregister(void *_cxlr_pmem)
  2607. {
  2608. struct cxl_pmem_region *cxlr_pmem = _cxlr_pmem;
  2609. struct cxl_region *cxlr = cxlr_pmem->cxlr;
  2610. struct cxl_nvdimm_bridge *cxl_nvb = cxlr->cxl_nvb;
  2611. /*
  2612. * Either the bridge is in ->remove() context under the device_lock(),
  2613. * or cxlr_release_nvdimm() is cancelling the bridge's release action
  2614. * for @cxlr_pmem and doing it itself (while manually holding the bridge
  2615. * lock).
  2616. */
  2617. device_lock_assert(&cxl_nvb->dev);
  2618. cxlr->cxlr_pmem = NULL;
  2619. cxlr_pmem->cxlr = NULL;
  2620. device_unregister(&cxlr_pmem->dev);
  2621. }
  2622. static void cxlr_release_nvdimm(void *_cxlr)
  2623. {
  2624. struct cxl_region *cxlr = _cxlr;
  2625. struct cxl_nvdimm_bridge *cxl_nvb = cxlr->cxl_nvb;
  2626. scoped_guard(device, &cxl_nvb->dev) {
  2627. if (cxlr->cxlr_pmem)
  2628. devm_release_action(&cxl_nvb->dev, cxlr_pmem_unregister,
  2629. cxlr->cxlr_pmem);
  2630. }
  2631. cxlr->cxl_nvb = NULL;
  2632. put_device(&cxl_nvb->dev);
  2633. }
  2634. /**
  2635. * devm_cxl_add_pmem_region() - add a cxl_region-to-nd_region bridge
  2636. * @cxlr: parent CXL region for this pmem region bridge device
  2637. *
  2638. * Return: 0 on success negative error code on failure.
  2639. */
  2640. static int devm_cxl_add_pmem_region(struct cxl_region *cxlr)
  2641. {
  2642. struct cxl_pmem_region *cxlr_pmem;
  2643. struct cxl_nvdimm_bridge *cxl_nvb;
  2644. struct device *dev;
  2645. int rc;
  2646. rc = cxl_pmem_region_alloc(cxlr);
  2647. if (rc)
  2648. return rc;
  2649. cxlr_pmem = cxlr->cxlr_pmem;
  2650. cxl_nvb = cxlr->cxl_nvb;
  2651. dev = &cxlr_pmem->dev;
  2652. rc = dev_set_name(dev, "pmem_region%d", cxlr->id);
  2653. if (rc)
  2654. goto err;
  2655. rc = device_add(dev);
  2656. if (rc)
  2657. goto err;
  2658. dev_dbg(&cxlr->dev, "%s: register %s\n", dev_name(dev->parent),
  2659. dev_name(dev));
  2660. scoped_guard(device, &cxl_nvb->dev) {
  2661. if (cxl_nvb->dev.driver)
  2662. rc = devm_add_action_or_reset(&cxl_nvb->dev,
  2663. cxlr_pmem_unregister,
  2664. cxlr_pmem);
  2665. else
  2666. rc = -ENXIO;
  2667. }
  2668. if (rc)
  2669. goto err_bridge;
  2670. /* @cxlr carries a reference on @cxl_nvb until cxlr_release_nvdimm */
  2671. return devm_add_action_or_reset(&cxlr->dev, cxlr_release_nvdimm, cxlr);
  2672. err:
  2673. put_device(dev);
  2674. err_bridge:
  2675. put_device(&cxl_nvb->dev);
  2676. cxlr->cxl_nvb = NULL;
  2677. return rc;
  2678. }
  2679. static void cxlr_dax_unregister(void *_cxlr_dax)
  2680. {
  2681. struct cxl_dax_region *cxlr_dax = _cxlr_dax;
  2682. device_unregister(&cxlr_dax->dev);
  2683. }
  2684. static int devm_cxl_add_dax_region(struct cxl_region *cxlr)
  2685. {
  2686. struct cxl_dax_region *cxlr_dax;
  2687. struct device *dev;
  2688. int rc;
  2689. cxlr_dax = cxl_dax_region_alloc(cxlr);
  2690. if (IS_ERR(cxlr_dax))
  2691. return PTR_ERR(cxlr_dax);
  2692. dev = &cxlr_dax->dev;
  2693. rc = dev_set_name(dev, "dax_region%d", cxlr->id);
  2694. if (rc)
  2695. goto err;
  2696. rc = device_add(dev);
  2697. if (rc)
  2698. goto err;
  2699. dev_dbg(&cxlr->dev, "%s: register %s\n", dev_name(dev->parent),
  2700. dev_name(dev));
  2701. return devm_add_action_or_reset(&cxlr->dev, cxlr_dax_unregister,
  2702. cxlr_dax);
  2703. err:
  2704. put_device(dev);
  2705. return rc;
  2706. }
  2707. static int match_root_decoder_by_range(struct device *dev, void *data)
  2708. {
  2709. struct range *r1, *r2 = data;
  2710. struct cxl_root_decoder *cxlrd;
  2711. if (!is_root_decoder(dev))
  2712. return 0;
  2713. cxlrd = to_cxl_root_decoder(dev);
  2714. r1 = &cxlrd->cxlsd.cxld.hpa_range;
  2715. return range_contains(r1, r2);
  2716. }
  2717. static int match_region_by_range(struct device *dev, void *data)
  2718. {
  2719. struct cxl_region_params *p;
  2720. struct cxl_region *cxlr;
  2721. struct range *r = data;
  2722. int rc = 0;
  2723. if (!is_cxl_region(dev))
  2724. return 0;
  2725. cxlr = to_cxl_region(dev);
  2726. p = &cxlr->params;
  2727. down_read(&cxl_region_rwsem);
  2728. if (p->res && p->res->start == r->start && p->res->end == r->end)
  2729. rc = 1;
  2730. up_read(&cxl_region_rwsem);
  2731. return rc;
  2732. }
  2733. /* Establish an empty region covering the given HPA range */
  2734. static struct cxl_region *construct_region(struct cxl_root_decoder *cxlrd,
  2735. struct cxl_endpoint_decoder *cxled)
  2736. {
  2737. struct cxl_memdev *cxlmd = cxled_to_memdev(cxled);
  2738. struct cxl_port *port = cxlrd_to_port(cxlrd);
  2739. struct range *hpa = &cxled->cxld.hpa_range;
  2740. struct cxl_region_params *p;
  2741. struct cxl_region *cxlr;
  2742. struct resource *res;
  2743. int rc;
  2744. do {
  2745. cxlr = __create_region(cxlrd, cxled->mode,
  2746. atomic_read(&cxlrd->region_id));
  2747. } while (IS_ERR(cxlr) && PTR_ERR(cxlr) == -EBUSY);
  2748. if (IS_ERR(cxlr)) {
  2749. dev_err(cxlmd->dev.parent,
  2750. "%s:%s: %s failed assign region: %ld\n",
  2751. dev_name(&cxlmd->dev), dev_name(&cxled->cxld.dev),
  2752. __func__, PTR_ERR(cxlr));
  2753. return cxlr;
  2754. }
  2755. down_write(&cxl_region_rwsem);
  2756. p = &cxlr->params;
  2757. if (p->state >= CXL_CONFIG_INTERLEAVE_ACTIVE) {
  2758. dev_err(cxlmd->dev.parent,
  2759. "%s:%s: %s autodiscovery interrupted\n",
  2760. dev_name(&cxlmd->dev), dev_name(&cxled->cxld.dev),
  2761. __func__);
  2762. rc = -EBUSY;
  2763. goto err;
  2764. }
  2765. set_bit(CXL_REGION_F_AUTO, &cxlr->flags);
  2766. res = kmalloc(sizeof(*res), GFP_KERNEL);
  2767. if (!res) {
  2768. rc = -ENOMEM;
  2769. goto err;
  2770. }
  2771. *res = DEFINE_RES_MEM_NAMED(hpa->start, range_len(hpa),
  2772. dev_name(&cxlr->dev));
  2773. rc = insert_resource(cxlrd->res, res);
  2774. if (rc) {
  2775. /*
  2776. * Platform-firmware may not have split resources like "System
  2777. * RAM" on CXL window boundaries see cxl_region_iomem_release()
  2778. */
  2779. dev_warn(cxlmd->dev.parent,
  2780. "%s:%s: %s %s cannot insert resource\n",
  2781. dev_name(&cxlmd->dev), dev_name(&cxled->cxld.dev),
  2782. __func__, dev_name(&cxlr->dev));
  2783. }
  2784. p->res = res;
  2785. p->interleave_ways = cxled->cxld.interleave_ways;
  2786. p->interleave_granularity = cxled->cxld.interleave_granularity;
  2787. p->state = CXL_CONFIG_INTERLEAVE_ACTIVE;
  2788. rc = sysfs_update_group(&cxlr->dev.kobj, get_cxl_region_target_group());
  2789. if (rc)
  2790. goto err;
  2791. dev_dbg(cxlmd->dev.parent, "%s:%s: %s %s res: %pr iw: %d ig: %d\n",
  2792. dev_name(&cxlmd->dev), dev_name(&cxled->cxld.dev), __func__,
  2793. dev_name(&cxlr->dev), p->res, p->interleave_ways,
  2794. p->interleave_granularity);
  2795. /* ...to match put_device() in cxl_add_to_region() */
  2796. get_device(&cxlr->dev);
  2797. up_write(&cxl_region_rwsem);
  2798. return cxlr;
  2799. err:
  2800. up_write(&cxl_region_rwsem);
  2801. devm_release_action(port->uport_dev, unregister_region, cxlr);
  2802. return ERR_PTR(rc);
  2803. }
  2804. int cxl_add_to_region(struct cxl_port *root, struct cxl_endpoint_decoder *cxled)
  2805. {
  2806. struct cxl_memdev *cxlmd = cxled_to_memdev(cxled);
  2807. struct range *hpa = &cxled->cxld.hpa_range;
  2808. struct cxl_decoder *cxld = &cxled->cxld;
  2809. struct device *cxlrd_dev, *region_dev;
  2810. struct cxl_root_decoder *cxlrd;
  2811. struct cxl_region_params *p;
  2812. struct cxl_region *cxlr;
  2813. bool attach = false;
  2814. int rc;
  2815. cxlrd_dev = device_find_child(&root->dev, &cxld->hpa_range,
  2816. match_root_decoder_by_range);
  2817. if (!cxlrd_dev) {
  2818. dev_err(cxlmd->dev.parent,
  2819. "%s:%s no CXL window for range %#llx:%#llx\n",
  2820. dev_name(&cxlmd->dev), dev_name(&cxld->dev),
  2821. cxld->hpa_range.start, cxld->hpa_range.end);
  2822. return -ENXIO;
  2823. }
  2824. cxlrd = to_cxl_root_decoder(cxlrd_dev);
  2825. /*
  2826. * Ensure that if multiple threads race to construct_region() for @hpa
  2827. * one does the construction and the others add to that.
  2828. */
  2829. mutex_lock(&cxlrd->range_lock);
  2830. region_dev = device_find_child(&cxlrd->cxlsd.cxld.dev, hpa,
  2831. match_region_by_range);
  2832. if (!region_dev) {
  2833. cxlr = construct_region(cxlrd, cxled);
  2834. region_dev = &cxlr->dev;
  2835. } else
  2836. cxlr = to_cxl_region(region_dev);
  2837. mutex_unlock(&cxlrd->range_lock);
  2838. rc = PTR_ERR_OR_ZERO(cxlr);
  2839. if (rc)
  2840. goto out;
  2841. attach_target(cxlr, cxled, -1, TASK_UNINTERRUPTIBLE);
  2842. down_read(&cxl_region_rwsem);
  2843. p = &cxlr->params;
  2844. attach = p->state == CXL_CONFIG_COMMIT;
  2845. up_read(&cxl_region_rwsem);
  2846. if (attach) {
  2847. /*
  2848. * If device_attach() fails the range may still be active via
  2849. * the platform-firmware memory map, otherwise the driver for
  2850. * regions is local to this file, so driver matching can't fail.
  2851. */
  2852. if (device_attach(&cxlr->dev) < 0)
  2853. dev_err(&cxlr->dev, "failed to enable, range: %pr\n",
  2854. p->res);
  2855. }
  2856. put_device(region_dev);
  2857. out:
  2858. put_device(cxlrd_dev);
  2859. return rc;
  2860. }
  2861. EXPORT_SYMBOL_NS_GPL(cxl_add_to_region, CXL);
  2862. static int is_system_ram(struct resource *res, void *arg)
  2863. {
  2864. struct cxl_region *cxlr = arg;
  2865. struct cxl_region_params *p = &cxlr->params;
  2866. dev_dbg(&cxlr->dev, "%pr has System RAM: %pr\n", p->res, res);
  2867. return 1;
  2868. }
  2869. static void shutdown_notifiers(void *_cxlr)
  2870. {
  2871. struct cxl_region *cxlr = _cxlr;
  2872. unregister_memory_notifier(&cxlr->memory_notifier);
  2873. unregister_mt_adistance_algorithm(&cxlr->adist_notifier);
  2874. }
  2875. static int cxl_region_probe(struct device *dev)
  2876. {
  2877. struct cxl_region *cxlr = to_cxl_region(dev);
  2878. struct cxl_region_params *p = &cxlr->params;
  2879. int rc;
  2880. rc = down_read_interruptible(&cxl_region_rwsem);
  2881. if (rc) {
  2882. dev_dbg(&cxlr->dev, "probe interrupted\n");
  2883. return rc;
  2884. }
  2885. if (p->state < CXL_CONFIG_COMMIT) {
  2886. dev_dbg(&cxlr->dev, "config state: %d\n", p->state);
  2887. rc = -ENXIO;
  2888. goto out;
  2889. }
  2890. if (test_bit(CXL_REGION_F_NEEDS_RESET, &cxlr->flags)) {
  2891. dev_err(&cxlr->dev,
  2892. "failed to activate, re-commit region and retry\n");
  2893. rc = -ENXIO;
  2894. goto out;
  2895. }
  2896. /*
  2897. * From this point on any path that changes the region's state away from
  2898. * CXL_CONFIG_COMMIT is also responsible for releasing the driver.
  2899. */
  2900. out:
  2901. up_read(&cxl_region_rwsem);
  2902. if (rc)
  2903. return rc;
  2904. cxlr->memory_notifier.notifier_call = cxl_region_perf_attrs_callback;
  2905. cxlr->memory_notifier.priority = CXL_CALLBACK_PRI;
  2906. register_memory_notifier(&cxlr->memory_notifier);
  2907. cxlr->adist_notifier.notifier_call = cxl_region_calculate_adistance;
  2908. cxlr->adist_notifier.priority = 100;
  2909. register_mt_adistance_algorithm(&cxlr->adist_notifier);
  2910. rc = devm_add_action_or_reset(&cxlr->dev, shutdown_notifiers, cxlr);
  2911. if (rc)
  2912. return rc;
  2913. switch (cxlr->mode) {
  2914. case CXL_DECODER_PMEM:
  2915. return devm_cxl_add_pmem_region(cxlr);
  2916. case CXL_DECODER_RAM:
  2917. /*
  2918. * The region can not be manged by CXL if any portion of
  2919. * it is already online as 'System RAM'
  2920. */
  2921. if (walk_iomem_res_desc(IORES_DESC_NONE,
  2922. IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY,
  2923. p->res->start, p->res->end, cxlr,
  2924. is_system_ram) > 0)
  2925. return 0;
  2926. return devm_cxl_add_dax_region(cxlr);
  2927. default:
  2928. dev_dbg(&cxlr->dev, "unsupported region mode: %d\n",
  2929. cxlr->mode);
  2930. return -ENXIO;
  2931. }
  2932. }
  2933. static struct cxl_driver cxl_region_driver = {
  2934. .name = "cxl_region",
  2935. .probe = cxl_region_probe,
  2936. .id = CXL_DEVICE_REGION,
  2937. };
  2938. int cxl_region_init(void)
  2939. {
  2940. return cxl_driver_register(&cxl_region_driver);
  2941. }
  2942. void cxl_region_exit(void)
  2943. {
  2944. cxl_driver_unregister(&cxl_region_driver);
  2945. }
  2946. MODULE_IMPORT_NS(CXL);
  2947. MODULE_IMPORT_NS(DEVMEM);
  2948. MODULE_ALIAS_CXL(CXL_DEVICE_REGION);