cxl.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. // Copyright(c) 2021 Intel Corporation. All rights reserved.
  3. #include <linux/platform_device.h>
  4. #include <linux/genalloc.h>
  5. #include <linux/module.h>
  6. #include <linux/mutex.h>
  7. #include <linux/acpi.h>
  8. #include <linux/pci.h>
  9. #include <linux/mm.h>
  10. #include <cxlmem.h>
  11. #include "../watermark.h"
  12. #include "mock.h"
  13. static int interleave_arithmetic;
  14. #define FAKE_QTG_ID 42
  15. #define NR_CXL_HOST_BRIDGES 2
  16. #define NR_CXL_SINGLE_HOST 1
  17. #define NR_CXL_RCH 1
  18. #define NR_CXL_ROOT_PORTS 2
  19. #define NR_CXL_SWITCH_PORTS 2
  20. #define NR_CXL_PORT_DECODERS 8
  21. #define NR_BRIDGES (NR_CXL_HOST_BRIDGES + NR_CXL_SINGLE_HOST + NR_CXL_RCH)
  22. static struct platform_device *cxl_acpi;
  23. static struct platform_device *cxl_host_bridge[NR_CXL_HOST_BRIDGES];
  24. #define NR_MULTI_ROOT (NR_CXL_HOST_BRIDGES * NR_CXL_ROOT_PORTS)
  25. static struct platform_device *cxl_root_port[NR_MULTI_ROOT];
  26. static struct platform_device *cxl_switch_uport[NR_MULTI_ROOT];
  27. #define NR_MEM_MULTI \
  28. (NR_CXL_HOST_BRIDGES * NR_CXL_ROOT_PORTS * NR_CXL_SWITCH_PORTS)
  29. static struct platform_device *cxl_switch_dport[NR_MEM_MULTI];
  30. static struct platform_device *cxl_hb_single[NR_CXL_SINGLE_HOST];
  31. static struct platform_device *cxl_root_single[NR_CXL_SINGLE_HOST];
  32. static struct platform_device *cxl_swu_single[NR_CXL_SINGLE_HOST];
  33. #define NR_MEM_SINGLE (NR_CXL_SINGLE_HOST * NR_CXL_SWITCH_PORTS)
  34. static struct platform_device *cxl_swd_single[NR_MEM_SINGLE];
  35. struct platform_device *cxl_mem[NR_MEM_MULTI];
  36. struct platform_device *cxl_mem_single[NR_MEM_SINGLE];
  37. static struct platform_device *cxl_rch[NR_CXL_RCH];
  38. static struct platform_device *cxl_rcd[NR_CXL_RCH];
  39. static inline bool is_multi_bridge(struct device *dev)
  40. {
  41. int i;
  42. for (i = 0; i < ARRAY_SIZE(cxl_host_bridge); i++)
  43. if (&cxl_host_bridge[i]->dev == dev)
  44. return true;
  45. return false;
  46. }
  47. static inline bool is_single_bridge(struct device *dev)
  48. {
  49. int i;
  50. for (i = 0; i < ARRAY_SIZE(cxl_hb_single); i++)
  51. if (&cxl_hb_single[i]->dev == dev)
  52. return true;
  53. return false;
  54. }
  55. static struct acpi_device acpi0017_mock;
  56. static struct acpi_device host_bridge[NR_BRIDGES] = {
  57. [0] = {
  58. .handle = &host_bridge[0],
  59. .pnp.unique_id = "0",
  60. },
  61. [1] = {
  62. .handle = &host_bridge[1],
  63. .pnp.unique_id = "1",
  64. },
  65. [2] = {
  66. .handle = &host_bridge[2],
  67. .pnp.unique_id = "2",
  68. },
  69. [3] = {
  70. .handle = &host_bridge[3],
  71. .pnp.unique_id = "3",
  72. },
  73. };
  74. static bool is_mock_dev(struct device *dev)
  75. {
  76. int i;
  77. for (i = 0; i < ARRAY_SIZE(cxl_mem); i++)
  78. if (dev == &cxl_mem[i]->dev)
  79. return true;
  80. for (i = 0; i < ARRAY_SIZE(cxl_mem_single); i++)
  81. if (dev == &cxl_mem_single[i]->dev)
  82. return true;
  83. for (i = 0; i < ARRAY_SIZE(cxl_rcd); i++)
  84. if (dev == &cxl_rcd[i]->dev)
  85. return true;
  86. if (dev == &cxl_acpi->dev)
  87. return true;
  88. return false;
  89. }
  90. static bool is_mock_adev(struct acpi_device *adev)
  91. {
  92. int i;
  93. if (adev == &acpi0017_mock)
  94. return true;
  95. for (i = 0; i < ARRAY_SIZE(host_bridge); i++)
  96. if (adev == &host_bridge[i])
  97. return true;
  98. return false;
  99. }
  100. static struct {
  101. struct acpi_table_cedt cedt;
  102. struct acpi_cedt_chbs chbs[NR_BRIDGES];
  103. struct {
  104. struct acpi_cedt_cfmws cfmws;
  105. u32 target[1];
  106. } cfmws0;
  107. struct {
  108. struct acpi_cedt_cfmws cfmws;
  109. u32 target[2];
  110. } cfmws1;
  111. struct {
  112. struct acpi_cedt_cfmws cfmws;
  113. u32 target[1];
  114. } cfmws2;
  115. struct {
  116. struct acpi_cedt_cfmws cfmws;
  117. u32 target[2];
  118. } cfmws3;
  119. struct {
  120. struct acpi_cedt_cfmws cfmws;
  121. u32 target[1];
  122. } cfmws4;
  123. struct {
  124. struct acpi_cedt_cfmws cfmws;
  125. u32 target[1];
  126. } cfmws5;
  127. struct {
  128. struct acpi_cedt_cfmws cfmws;
  129. u32 target[1];
  130. } cfmws6;
  131. struct {
  132. struct acpi_cedt_cfmws cfmws;
  133. u32 target[2];
  134. } cfmws7;
  135. struct {
  136. struct acpi_cedt_cfmws cfmws;
  137. u32 target[4];
  138. } cfmws8;
  139. struct {
  140. struct acpi_cedt_cxims cxims;
  141. u64 xormap_list[2];
  142. } cxims0;
  143. } __packed mock_cedt = {
  144. .cedt = {
  145. .header = {
  146. .signature = "CEDT",
  147. .length = sizeof(mock_cedt),
  148. .revision = 1,
  149. },
  150. },
  151. .chbs[0] = {
  152. .header = {
  153. .type = ACPI_CEDT_TYPE_CHBS,
  154. .length = sizeof(mock_cedt.chbs[0]),
  155. },
  156. .uid = 0,
  157. .cxl_version = ACPI_CEDT_CHBS_VERSION_CXL20,
  158. },
  159. .chbs[1] = {
  160. .header = {
  161. .type = ACPI_CEDT_TYPE_CHBS,
  162. .length = sizeof(mock_cedt.chbs[0]),
  163. },
  164. .uid = 1,
  165. .cxl_version = ACPI_CEDT_CHBS_VERSION_CXL20,
  166. },
  167. .chbs[2] = {
  168. .header = {
  169. .type = ACPI_CEDT_TYPE_CHBS,
  170. .length = sizeof(mock_cedt.chbs[0]),
  171. },
  172. .uid = 2,
  173. .cxl_version = ACPI_CEDT_CHBS_VERSION_CXL20,
  174. },
  175. .chbs[3] = {
  176. .header = {
  177. .type = ACPI_CEDT_TYPE_CHBS,
  178. .length = sizeof(mock_cedt.chbs[0]),
  179. },
  180. .uid = 3,
  181. .cxl_version = ACPI_CEDT_CHBS_VERSION_CXL11,
  182. },
  183. .cfmws0 = {
  184. .cfmws = {
  185. .header = {
  186. .type = ACPI_CEDT_TYPE_CFMWS,
  187. .length = sizeof(mock_cedt.cfmws0),
  188. },
  189. .interleave_ways = 0,
  190. .granularity = 4,
  191. .restrictions = ACPI_CEDT_CFMWS_RESTRICT_TYPE3 |
  192. ACPI_CEDT_CFMWS_RESTRICT_VOLATILE,
  193. .qtg_id = FAKE_QTG_ID,
  194. .window_size = SZ_256M * 4UL,
  195. },
  196. .target = { 0 },
  197. },
  198. .cfmws1 = {
  199. .cfmws = {
  200. .header = {
  201. .type = ACPI_CEDT_TYPE_CFMWS,
  202. .length = sizeof(mock_cedt.cfmws1),
  203. },
  204. .interleave_ways = 1,
  205. .granularity = 4,
  206. .restrictions = ACPI_CEDT_CFMWS_RESTRICT_TYPE3 |
  207. ACPI_CEDT_CFMWS_RESTRICT_VOLATILE,
  208. .qtg_id = FAKE_QTG_ID,
  209. .window_size = SZ_256M * 8UL,
  210. },
  211. .target = { 0, 1, },
  212. },
  213. .cfmws2 = {
  214. .cfmws = {
  215. .header = {
  216. .type = ACPI_CEDT_TYPE_CFMWS,
  217. .length = sizeof(mock_cedt.cfmws2),
  218. },
  219. .interleave_ways = 0,
  220. .granularity = 4,
  221. .restrictions = ACPI_CEDT_CFMWS_RESTRICT_TYPE3 |
  222. ACPI_CEDT_CFMWS_RESTRICT_PMEM,
  223. .qtg_id = FAKE_QTG_ID,
  224. .window_size = SZ_256M * 4UL,
  225. },
  226. .target = { 0 },
  227. },
  228. .cfmws3 = {
  229. .cfmws = {
  230. .header = {
  231. .type = ACPI_CEDT_TYPE_CFMWS,
  232. .length = sizeof(mock_cedt.cfmws3),
  233. },
  234. .interleave_ways = 1,
  235. .granularity = 4,
  236. .restrictions = ACPI_CEDT_CFMWS_RESTRICT_TYPE3 |
  237. ACPI_CEDT_CFMWS_RESTRICT_PMEM,
  238. .qtg_id = FAKE_QTG_ID,
  239. .window_size = SZ_256M * 8UL,
  240. },
  241. .target = { 0, 1, },
  242. },
  243. .cfmws4 = {
  244. .cfmws = {
  245. .header = {
  246. .type = ACPI_CEDT_TYPE_CFMWS,
  247. .length = sizeof(mock_cedt.cfmws4),
  248. },
  249. .interleave_ways = 0,
  250. .granularity = 4,
  251. .restrictions = ACPI_CEDT_CFMWS_RESTRICT_TYPE3 |
  252. ACPI_CEDT_CFMWS_RESTRICT_PMEM,
  253. .qtg_id = FAKE_QTG_ID,
  254. .window_size = SZ_256M * 4UL,
  255. },
  256. .target = { 2 },
  257. },
  258. .cfmws5 = {
  259. .cfmws = {
  260. .header = {
  261. .type = ACPI_CEDT_TYPE_CFMWS,
  262. .length = sizeof(mock_cedt.cfmws5),
  263. },
  264. .interleave_ways = 0,
  265. .granularity = 4,
  266. .restrictions = ACPI_CEDT_CFMWS_RESTRICT_TYPE3 |
  267. ACPI_CEDT_CFMWS_RESTRICT_VOLATILE,
  268. .qtg_id = FAKE_QTG_ID,
  269. .window_size = SZ_256M,
  270. },
  271. .target = { 3 },
  272. },
  273. /* .cfmws6,7,8 use ACPI_CEDT_CFMWS_ARITHMETIC_XOR */
  274. .cfmws6 = {
  275. .cfmws = {
  276. .header = {
  277. .type = ACPI_CEDT_TYPE_CFMWS,
  278. .length = sizeof(mock_cedt.cfmws6),
  279. },
  280. .interleave_arithmetic = ACPI_CEDT_CFMWS_ARITHMETIC_XOR,
  281. .interleave_ways = 0,
  282. .granularity = 4,
  283. .restrictions = ACPI_CEDT_CFMWS_RESTRICT_TYPE3 |
  284. ACPI_CEDT_CFMWS_RESTRICT_PMEM,
  285. .qtg_id = FAKE_QTG_ID,
  286. .window_size = SZ_256M * 8UL,
  287. },
  288. .target = { 0, },
  289. },
  290. .cfmws7 = {
  291. .cfmws = {
  292. .header = {
  293. .type = ACPI_CEDT_TYPE_CFMWS,
  294. .length = sizeof(mock_cedt.cfmws7),
  295. },
  296. .interleave_arithmetic = ACPI_CEDT_CFMWS_ARITHMETIC_XOR,
  297. .interleave_ways = 1,
  298. .granularity = 0,
  299. .restrictions = ACPI_CEDT_CFMWS_RESTRICT_TYPE3 |
  300. ACPI_CEDT_CFMWS_RESTRICT_PMEM,
  301. .qtg_id = FAKE_QTG_ID,
  302. .window_size = SZ_256M * 8UL,
  303. },
  304. .target = { 0, 1, },
  305. },
  306. .cfmws8 = {
  307. .cfmws = {
  308. .header = {
  309. .type = ACPI_CEDT_TYPE_CFMWS,
  310. .length = sizeof(mock_cedt.cfmws8),
  311. },
  312. .interleave_arithmetic = ACPI_CEDT_CFMWS_ARITHMETIC_XOR,
  313. .interleave_ways = 2,
  314. .granularity = 0,
  315. .restrictions = ACPI_CEDT_CFMWS_RESTRICT_TYPE3 |
  316. ACPI_CEDT_CFMWS_RESTRICT_PMEM,
  317. .qtg_id = FAKE_QTG_ID,
  318. .window_size = SZ_256M * 16UL,
  319. },
  320. .target = { 0, 1, 0, 1, },
  321. },
  322. .cxims0 = {
  323. .cxims = {
  324. .header = {
  325. .type = ACPI_CEDT_TYPE_CXIMS,
  326. .length = sizeof(mock_cedt.cxims0),
  327. },
  328. .hbig = 0,
  329. .nr_xormaps = 2,
  330. },
  331. .xormap_list = { 0x404100, 0x808200, },
  332. },
  333. };
  334. struct acpi_cedt_cfmws *mock_cfmws[] = {
  335. [0] = &mock_cedt.cfmws0.cfmws,
  336. [1] = &mock_cedt.cfmws1.cfmws,
  337. [2] = &mock_cedt.cfmws2.cfmws,
  338. [3] = &mock_cedt.cfmws3.cfmws,
  339. [4] = &mock_cedt.cfmws4.cfmws,
  340. [5] = &mock_cedt.cfmws5.cfmws,
  341. /* Modulo Math above, XOR Math below */
  342. [6] = &mock_cedt.cfmws6.cfmws,
  343. [7] = &mock_cedt.cfmws7.cfmws,
  344. [8] = &mock_cedt.cfmws8.cfmws,
  345. };
  346. static int cfmws_start;
  347. static int cfmws_end;
  348. #define CFMWS_MOD_ARRAY_START 0
  349. #define CFMWS_MOD_ARRAY_END 5
  350. #define CFMWS_XOR_ARRAY_START 6
  351. #define CFMWS_XOR_ARRAY_END 8
  352. struct acpi_cedt_cxims *mock_cxims[1] = {
  353. [0] = &mock_cedt.cxims0.cxims,
  354. };
  355. struct cxl_mock_res {
  356. struct list_head list;
  357. struct range range;
  358. };
  359. static LIST_HEAD(mock_res);
  360. static DEFINE_MUTEX(mock_res_lock);
  361. static struct gen_pool *cxl_mock_pool;
  362. static void depopulate_all_mock_resources(void)
  363. {
  364. struct cxl_mock_res *res, *_res;
  365. mutex_lock(&mock_res_lock);
  366. list_for_each_entry_safe(res, _res, &mock_res, list) {
  367. gen_pool_free(cxl_mock_pool, res->range.start,
  368. range_len(&res->range));
  369. list_del(&res->list);
  370. kfree(res);
  371. }
  372. mutex_unlock(&mock_res_lock);
  373. }
  374. static struct cxl_mock_res *alloc_mock_res(resource_size_t size, int align)
  375. {
  376. struct cxl_mock_res *res = kzalloc(sizeof(*res), GFP_KERNEL);
  377. struct genpool_data_align data = {
  378. .align = align,
  379. };
  380. unsigned long phys;
  381. INIT_LIST_HEAD(&res->list);
  382. phys = gen_pool_alloc_algo(cxl_mock_pool, size,
  383. gen_pool_first_fit_align, &data);
  384. if (!phys)
  385. return NULL;
  386. res->range = (struct range) {
  387. .start = phys,
  388. .end = phys + size - 1,
  389. };
  390. mutex_lock(&mock_res_lock);
  391. list_add(&res->list, &mock_res);
  392. mutex_unlock(&mock_res_lock);
  393. return res;
  394. }
  395. static int populate_cedt(void)
  396. {
  397. struct cxl_mock_res *res;
  398. int i;
  399. for (i = 0; i < ARRAY_SIZE(mock_cedt.chbs); i++) {
  400. struct acpi_cedt_chbs *chbs = &mock_cedt.chbs[i];
  401. resource_size_t size;
  402. if (chbs->cxl_version == ACPI_CEDT_CHBS_VERSION_CXL20)
  403. size = ACPI_CEDT_CHBS_LENGTH_CXL20;
  404. else
  405. size = ACPI_CEDT_CHBS_LENGTH_CXL11;
  406. res = alloc_mock_res(size, size);
  407. if (!res)
  408. return -ENOMEM;
  409. chbs->base = res->range.start;
  410. chbs->length = size;
  411. }
  412. for (i = cfmws_start; i <= cfmws_end; i++) {
  413. struct acpi_cedt_cfmws *window = mock_cfmws[i];
  414. res = alloc_mock_res(window->window_size, SZ_256M);
  415. if (!res)
  416. return -ENOMEM;
  417. window->base_hpa = res->range.start;
  418. }
  419. return 0;
  420. }
  421. static bool is_mock_port(struct device *dev);
  422. /*
  423. * WARNING, this hack assumes the format of 'struct cxl_cfmws_context'
  424. * and 'struct cxl_chbs_context' share the property that the first
  425. * struct member is a cxl_test device being probed by the cxl_acpi
  426. * driver.
  427. */
  428. struct cxl_cedt_context {
  429. struct device *dev;
  430. };
  431. static int mock_acpi_table_parse_cedt(enum acpi_cedt_type id,
  432. acpi_tbl_entry_handler_arg handler_arg,
  433. void *arg)
  434. {
  435. struct cxl_cedt_context *ctx = arg;
  436. struct device *dev = ctx->dev;
  437. union acpi_subtable_headers *h;
  438. unsigned long end;
  439. int i;
  440. if (!is_mock_port(dev) && !is_mock_dev(dev))
  441. return acpi_table_parse_cedt(id, handler_arg, arg);
  442. if (id == ACPI_CEDT_TYPE_CHBS)
  443. for (i = 0; i < ARRAY_SIZE(mock_cedt.chbs); i++) {
  444. h = (union acpi_subtable_headers *)&mock_cedt.chbs[i];
  445. end = (unsigned long)&mock_cedt.chbs[i + 1];
  446. handler_arg(h, arg, end);
  447. }
  448. if (id == ACPI_CEDT_TYPE_CFMWS)
  449. for (i = cfmws_start; i <= cfmws_end; i++) {
  450. h = (union acpi_subtable_headers *) mock_cfmws[i];
  451. end = (unsigned long) h + mock_cfmws[i]->header.length;
  452. handler_arg(h, arg, end);
  453. }
  454. if (id == ACPI_CEDT_TYPE_CXIMS)
  455. for (i = 0; i < ARRAY_SIZE(mock_cxims); i++) {
  456. h = (union acpi_subtable_headers *)mock_cxims[i];
  457. end = (unsigned long)h + mock_cxims[i]->header.length;
  458. handler_arg(h, arg, end);
  459. }
  460. return 0;
  461. }
  462. static bool is_mock_bridge(struct device *dev)
  463. {
  464. int i;
  465. for (i = 0; i < ARRAY_SIZE(cxl_host_bridge); i++)
  466. if (dev == &cxl_host_bridge[i]->dev)
  467. return true;
  468. for (i = 0; i < ARRAY_SIZE(cxl_hb_single); i++)
  469. if (dev == &cxl_hb_single[i]->dev)
  470. return true;
  471. for (i = 0; i < ARRAY_SIZE(cxl_rch); i++)
  472. if (dev == &cxl_rch[i]->dev)
  473. return true;
  474. return false;
  475. }
  476. static bool is_mock_port(struct device *dev)
  477. {
  478. int i;
  479. if (is_mock_bridge(dev))
  480. return true;
  481. for (i = 0; i < ARRAY_SIZE(cxl_root_port); i++)
  482. if (dev == &cxl_root_port[i]->dev)
  483. return true;
  484. for (i = 0; i < ARRAY_SIZE(cxl_switch_uport); i++)
  485. if (dev == &cxl_switch_uport[i]->dev)
  486. return true;
  487. for (i = 0; i < ARRAY_SIZE(cxl_switch_dport); i++)
  488. if (dev == &cxl_switch_dport[i]->dev)
  489. return true;
  490. for (i = 0; i < ARRAY_SIZE(cxl_root_single); i++)
  491. if (dev == &cxl_root_single[i]->dev)
  492. return true;
  493. for (i = 0; i < ARRAY_SIZE(cxl_swu_single); i++)
  494. if (dev == &cxl_swu_single[i]->dev)
  495. return true;
  496. for (i = 0; i < ARRAY_SIZE(cxl_swd_single); i++)
  497. if (dev == &cxl_swd_single[i]->dev)
  498. return true;
  499. if (is_cxl_memdev(dev))
  500. return is_mock_dev(dev->parent);
  501. return false;
  502. }
  503. static int host_bridge_index(struct acpi_device *adev)
  504. {
  505. return adev - host_bridge;
  506. }
  507. static struct acpi_device *find_host_bridge(acpi_handle handle)
  508. {
  509. int i;
  510. for (i = 0; i < ARRAY_SIZE(host_bridge); i++)
  511. if (handle == host_bridge[i].handle)
  512. return &host_bridge[i];
  513. return NULL;
  514. }
  515. static acpi_status
  516. mock_acpi_evaluate_integer(acpi_handle handle, acpi_string pathname,
  517. struct acpi_object_list *arguments,
  518. unsigned long long *data)
  519. {
  520. struct acpi_device *adev = find_host_bridge(handle);
  521. if (!adev || strcmp(pathname, METHOD_NAME__UID) != 0)
  522. return acpi_evaluate_integer(handle, pathname, arguments, data);
  523. *data = host_bridge_index(adev);
  524. return AE_OK;
  525. }
  526. static struct pci_bus mock_pci_bus[NR_BRIDGES];
  527. static struct acpi_pci_root mock_pci_root[ARRAY_SIZE(mock_pci_bus)] = {
  528. [0] = {
  529. .bus = &mock_pci_bus[0],
  530. },
  531. [1] = {
  532. .bus = &mock_pci_bus[1],
  533. },
  534. [2] = {
  535. .bus = &mock_pci_bus[2],
  536. },
  537. [3] = {
  538. .bus = &mock_pci_bus[3],
  539. },
  540. };
  541. static bool is_mock_bus(struct pci_bus *bus)
  542. {
  543. int i;
  544. for (i = 0; i < ARRAY_SIZE(mock_pci_bus); i++)
  545. if (bus == &mock_pci_bus[i])
  546. return true;
  547. return false;
  548. }
  549. static struct acpi_pci_root *mock_acpi_pci_find_root(acpi_handle handle)
  550. {
  551. struct acpi_device *adev = find_host_bridge(handle);
  552. if (!adev)
  553. return acpi_pci_find_root(handle);
  554. return &mock_pci_root[host_bridge_index(adev)];
  555. }
  556. static struct cxl_hdm *mock_cxl_setup_hdm(struct cxl_port *port,
  557. struct cxl_endpoint_dvsec_info *info)
  558. {
  559. struct cxl_hdm *cxlhdm = devm_kzalloc(&port->dev, sizeof(*cxlhdm), GFP_KERNEL);
  560. struct device *dev = &port->dev;
  561. if (!cxlhdm)
  562. return ERR_PTR(-ENOMEM);
  563. cxlhdm->port = port;
  564. cxlhdm->interleave_mask = ~0U;
  565. cxlhdm->iw_cap_mask = ~0UL;
  566. dev_set_drvdata(dev, cxlhdm);
  567. return cxlhdm;
  568. }
  569. static int mock_cxl_add_passthrough_decoder(struct cxl_port *port)
  570. {
  571. dev_err(&port->dev, "unexpected passthrough decoder for cxl_test\n");
  572. return -EOPNOTSUPP;
  573. }
  574. struct target_map_ctx {
  575. int *target_map;
  576. int index;
  577. int target_count;
  578. };
  579. static int map_targets(struct device *dev, void *data)
  580. {
  581. struct platform_device *pdev = to_platform_device(dev);
  582. struct target_map_ctx *ctx = data;
  583. ctx->target_map[ctx->index++] = pdev->id;
  584. if (ctx->index > ctx->target_count) {
  585. dev_WARN_ONCE(dev, 1, "too many targets found?\n");
  586. return -ENXIO;
  587. }
  588. return 0;
  589. }
  590. static int mock_decoder_commit(struct cxl_decoder *cxld)
  591. {
  592. struct cxl_port *port = to_cxl_port(cxld->dev.parent);
  593. int id = cxld->id;
  594. if (cxld->flags & CXL_DECODER_F_ENABLE)
  595. return 0;
  596. dev_dbg(&port->dev, "%s commit\n", dev_name(&cxld->dev));
  597. if (cxl_num_decoders_committed(port) != id) {
  598. dev_dbg(&port->dev,
  599. "%s: out of order commit, expected decoder%d.%d\n",
  600. dev_name(&cxld->dev), port->id,
  601. cxl_num_decoders_committed(port));
  602. return -EBUSY;
  603. }
  604. port->commit_end++;
  605. cxld->flags |= CXL_DECODER_F_ENABLE;
  606. return 0;
  607. }
  608. static void mock_decoder_reset(struct cxl_decoder *cxld)
  609. {
  610. struct cxl_port *port = to_cxl_port(cxld->dev.parent);
  611. int id = cxld->id;
  612. if ((cxld->flags & CXL_DECODER_F_ENABLE) == 0)
  613. return;
  614. dev_dbg(&port->dev, "%s reset\n", dev_name(&cxld->dev));
  615. if (port->commit_end == id)
  616. cxl_port_commit_reap(cxld);
  617. else
  618. dev_dbg(&port->dev,
  619. "%s: out of order reset, expected decoder%d.%d\n",
  620. dev_name(&cxld->dev), port->id, port->commit_end);
  621. cxld->flags &= ~CXL_DECODER_F_ENABLE;
  622. }
  623. static void default_mock_decoder(struct cxl_decoder *cxld)
  624. {
  625. cxld->hpa_range = (struct range){
  626. .start = 0,
  627. .end = -1,
  628. };
  629. cxld->interleave_ways = 1;
  630. cxld->interleave_granularity = 256;
  631. cxld->target_type = CXL_DECODER_HOSTONLYMEM;
  632. cxld->commit = mock_decoder_commit;
  633. cxld->reset = mock_decoder_reset;
  634. }
  635. static int first_decoder(struct device *dev, void *data)
  636. {
  637. struct cxl_decoder *cxld;
  638. if (!is_switch_decoder(dev))
  639. return 0;
  640. cxld = to_cxl_decoder(dev);
  641. if (cxld->id == 0)
  642. return 1;
  643. return 0;
  644. }
  645. static void mock_init_hdm_decoder(struct cxl_decoder *cxld)
  646. {
  647. struct acpi_cedt_cfmws *window = mock_cfmws[0];
  648. struct platform_device *pdev = NULL;
  649. struct cxl_endpoint_decoder *cxled;
  650. struct cxl_switch_decoder *cxlsd;
  651. struct cxl_port *port, *iter;
  652. const int size = SZ_512M;
  653. struct cxl_memdev *cxlmd;
  654. struct cxl_dport *dport;
  655. struct device *dev;
  656. bool hb0 = false;
  657. u64 base;
  658. int i;
  659. if (is_endpoint_decoder(&cxld->dev)) {
  660. cxled = to_cxl_endpoint_decoder(&cxld->dev);
  661. cxlmd = cxled_to_memdev(cxled);
  662. WARN_ON(!dev_is_platform(cxlmd->dev.parent));
  663. pdev = to_platform_device(cxlmd->dev.parent);
  664. /* check is endpoint is attach to host-bridge0 */
  665. port = cxled_to_port(cxled);
  666. do {
  667. if (port->uport_dev == &cxl_host_bridge[0]->dev) {
  668. hb0 = true;
  669. break;
  670. }
  671. if (is_cxl_port(port->dev.parent))
  672. port = to_cxl_port(port->dev.parent);
  673. else
  674. port = NULL;
  675. } while (port);
  676. port = cxled_to_port(cxled);
  677. }
  678. /*
  679. * The first decoder on the first 2 devices on the first switch
  680. * attached to host-bridge0 mock a fake / static RAM region. All
  681. * other decoders are default disabled. Given the round robin
  682. * assignment those devices are named cxl_mem.0, and cxl_mem.4.
  683. *
  684. * See 'cxl list -BMPu -m cxl_mem.0,cxl_mem.4'
  685. */
  686. if (!hb0 || pdev->id % 4 || pdev->id > 4 || cxld->id > 0) {
  687. default_mock_decoder(cxld);
  688. return;
  689. }
  690. base = window->base_hpa;
  691. cxld->hpa_range = (struct range) {
  692. .start = base,
  693. .end = base + size - 1,
  694. };
  695. cxld->interleave_ways = 2;
  696. eig_to_granularity(window->granularity, &cxld->interleave_granularity);
  697. cxld->target_type = CXL_DECODER_HOSTONLYMEM;
  698. cxld->flags = CXL_DECODER_F_ENABLE;
  699. cxled->state = CXL_DECODER_STATE_AUTO;
  700. port->commit_end = cxld->id;
  701. devm_cxl_dpa_reserve(cxled, 0, size / cxld->interleave_ways, 0);
  702. cxld->commit = mock_decoder_commit;
  703. cxld->reset = mock_decoder_reset;
  704. /*
  705. * Now that endpoint decoder is set up, walk up the hierarchy
  706. * and setup the switch and root port decoders targeting @cxlmd.
  707. */
  708. iter = port;
  709. for (i = 0; i < 2; i++) {
  710. dport = iter->parent_dport;
  711. iter = dport->port;
  712. dev = device_find_child(&iter->dev, NULL, first_decoder);
  713. /*
  714. * Ancestor ports are guaranteed to be enumerated before
  715. * @port, and all ports have at least one decoder.
  716. */
  717. if (WARN_ON(!dev))
  718. continue;
  719. cxlsd = to_cxl_switch_decoder(dev);
  720. if (i == 0) {
  721. /* put cxl_mem.4 second in the decode order */
  722. if (pdev->id == 4)
  723. cxlsd->target[1] = dport;
  724. else
  725. cxlsd->target[0] = dport;
  726. } else
  727. cxlsd->target[0] = dport;
  728. cxld = &cxlsd->cxld;
  729. cxld->target_type = CXL_DECODER_HOSTONLYMEM;
  730. cxld->flags = CXL_DECODER_F_ENABLE;
  731. iter->commit_end = 0;
  732. /*
  733. * Switch targets 2 endpoints, while host bridge targets
  734. * one root port
  735. */
  736. if (i == 0)
  737. cxld->interleave_ways = 2;
  738. else
  739. cxld->interleave_ways = 1;
  740. cxld->interleave_granularity = 4096;
  741. cxld->hpa_range = (struct range) {
  742. .start = base,
  743. .end = base + size - 1,
  744. };
  745. put_device(dev);
  746. }
  747. }
  748. static int mock_cxl_enumerate_decoders(struct cxl_hdm *cxlhdm,
  749. struct cxl_endpoint_dvsec_info *info)
  750. {
  751. struct cxl_port *port = cxlhdm->port;
  752. struct cxl_port *parent_port = to_cxl_port(port->dev.parent);
  753. int target_count, i;
  754. if (is_cxl_endpoint(port))
  755. target_count = 0;
  756. else if (is_cxl_root(parent_port))
  757. target_count = NR_CXL_ROOT_PORTS;
  758. else
  759. target_count = NR_CXL_SWITCH_PORTS;
  760. for (i = 0; i < NR_CXL_PORT_DECODERS; i++) {
  761. int target_map[CXL_DECODER_MAX_INTERLEAVE] = { 0 };
  762. struct target_map_ctx ctx = {
  763. .target_map = target_map,
  764. .target_count = target_count,
  765. };
  766. struct cxl_decoder *cxld;
  767. int rc;
  768. if (target_count) {
  769. struct cxl_switch_decoder *cxlsd;
  770. cxlsd = cxl_switch_decoder_alloc(port, target_count);
  771. if (IS_ERR(cxlsd)) {
  772. dev_warn(&port->dev,
  773. "Failed to allocate the decoder\n");
  774. return PTR_ERR(cxlsd);
  775. }
  776. cxld = &cxlsd->cxld;
  777. } else {
  778. struct cxl_endpoint_decoder *cxled;
  779. cxled = cxl_endpoint_decoder_alloc(port);
  780. if (IS_ERR(cxled)) {
  781. dev_warn(&port->dev,
  782. "Failed to allocate the decoder\n");
  783. return PTR_ERR(cxled);
  784. }
  785. cxld = &cxled->cxld;
  786. }
  787. mock_init_hdm_decoder(cxld);
  788. if (target_count) {
  789. rc = device_for_each_child(port->uport_dev, &ctx,
  790. map_targets);
  791. if (rc) {
  792. put_device(&cxld->dev);
  793. return rc;
  794. }
  795. }
  796. rc = cxl_decoder_add_locked(cxld, target_map);
  797. if (rc) {
  798. put_device(&cxld->dev);
  799. dev_err(&port->dev, "Failed to add decoder\n");
  800. return rc;
  801. }
  802. rc = cxl_decoder_autoremove(&port->dev, cxld);
  803. if (rc)
  804. return rc;
  805. dev_dbg(&cxld->dev, "Added to port %s\n", dev_name(&port->dev));
  806. }
  807. return 0;
  808. }
  809. static int mock_cxl_port_enumerate_dports(struct cxl_port *port)
  810. {
  811. struct platform_device **array;
  812. int i, array_size;
  813. if (port->depth == 1) {
  814. if (is_multi_bridge(port->uport_dev)) {
  815. array_size = ARRAY_SIZE(cxl_root_port);
  816. array = cxl_root_port;
  817. } else if (is_single_bridge(port->uport_dev)) {
  818. array_size = ARRAY_SIZE(cxl_root_single);
  819. array = cxl_root_single;
  820. } else {
  821. dev_dbg(&port->dev, "%s: unknown bridge type\n",
  822. dev_name(port->uport_dev));
  823. return -ENXIO;
  824. }
  825. } else if (port->depth == 2) {
  826. struct cxl_port *parent = to_cxl_port(port->dev.parent);
  827. if (is_multi_bridge(parent->uport_dev)) {
  828. array_size = ARRAY_SIZE(cxl_switch_dport);
  829. array = cxl_switch_dport;
  830. } else if (is_single_bridge(parent->uport_dev)) {
  831. array_size = ARRAY_SIZE(cxl_swd_single);
  832. array = cxl_swd_single;
  833. } else {
  834. dev_dbg(&port->dev, "%s: unknown bridge type\n",
  835. dev_name(port->uport_dev));
  836. return -ENXIO;
  837. }
  838. } else {
  839. dev_WARN_ONCE(&port->dev, 1, "unexpected depth %d\n",
  840. port->depth);
  841. return -ENXIO;
  842. }
  843. for (i = 0; i < array_size; i++) {
  844. struct platform_device *pdev = array[i];
  845. struct cxl_dport *dport;
  846. if (pdev->dev.parent != port->uport_dev) {
  847. dev_dbg(&port->dev, "%s: mismatch parent %s\n",
  848. dev_name(port->uport_dev),
  849. dev_name(pdev->dev.parent));
  850. continue;
  851. }
  852. dport = devm_cxl_add_dport(port, &pdev->dev, pdev->id,
  853. CXL_RESOURCE_NONE);
  854. if (IS_ERR(dport))
  855. return PTR_ERR(dport);
  856. }
  857. return 0;
  858. }
  859. /*
  860. * Faking the cxl_dpa_perf for the memdev when appropriate.
  861. */
  862. static void dpa_perf_setup(struct cxl_port *endpoint, struct range *range,
  863. struct cxl_dpa_perf *dpa_perf)
  864. {
  865. dpa_perf->qos_class = FAKE_QTG_ID;
  866. dpa_perf->dpa_range = *range;
  867. for (int i = 0; i < ACCESS_COORDINATE_MAX; i++) {
  868. dpa_perf->coord[i].read_latency = 500;
  869. dpa_perf->coord[i].write_latency = 500;
  870. dpa_perf->coord[i].read_bandwidth = 1000;
  871. dpa_perf->coord[i].write_bandwidth = 1000;
  872. }
  873. }
  874. static void mock_cxl_endpoint_parse_cdat(struct cxl_port *port)
  875. {
  876. struct cxl_root *cxl_root __free(put_cxl_root) =
  877. find_cxl_root(port);
  878. struct cxl_memdev *cxlmd = to_cxl_memdev(port->uport_dev);
  879. struct cxl_dev_state *cxlds = cxlmd->cxlds;
  880. struct cxl_memdev_state *mds = to_cxl_memdev_state(cxlds);
  881. struct access_coordinate ep_c[ACCESS_COORDINATE_MAX];
  882. struct range pmem_range = {
  883. .start = cxlds->pmem_res.start,
  884. .end = cxlds->pmem_res.end,
  885. };
  886. struct range ram_range = {
  887. .start = cxlds->ram_res.start,
  888. .end = cxlds->ram_res.end,
  889. };
  890. if (!cxl_root)
  891. return;
  892. if (range_len(&ram_range))
  893. dpa_perf_setup(port, &ram_range, &mds->ram_perf);
  894. if (range_len(&pmem_range))
  895. dpa_perf_setup(port, &pmem_range, &mds->pmem_perf);
  896. cxl_memdev_update_perf(cxlmd);
  897. /*
  898. * This function is here to only test the topology iterator. It serves
  899. * no other purpose.
  900. */
  901. cxl_endpoint_get_perf_coordinates(port, ep_c);
  902. }
  903. static struct cxl_mock_ops cxl_mock_ops = {
  904. .is_mock_adev = is_mock_adev,
  905. .is_mock_bridge = is_mock_bridge,
  906. .is_mock_bus = is_mock_bus,
  907. .is_mock_port = is_mock_port,
  908. .is_mock_dev = is_mock_dev,
  909. .acpi_table_parse_cedt = mock_acpi_table_parse_cedt,
  910. .acpi_evaluate_integer = mock_acpi_evaluate_integer,
  911. .acpi_pci_find_root = mock_acpi_pci_find_root,
  912. .devm_cxl_port_enumerate_dports = mock_cxl_port_enumerate_dports,
  913. .devm_cxl_setup_hdm = mock_cxl_setup_hdm,
  914. .devm_cxl_add_passthrough_decoder = mock_cxl_add_passthrough_decoder,
  915. .devm_cxl_enumerate_decoders = mock_cxl_enumerate_decoders,
  916. .cxl_endpoint_parse_cdat = mock_cxl_endpoint_parse_cdat,
  917. .list = LIST_HEAD_INIT(cxl_mock_ops.list),
  918. };
  919. static void mock_companion(struct acpi_device *adev, struct device *dev)
  920. {
  921. device_initialize(&adev->dev);
  922. fwnode_init(&adev->fwnode, NULL);
  923. dev->fwnode = &adev->fwnode;
  924. adev->fwnode.dev = dev;
  925. }
  926. #ifndef SZ_64G
  927. #define SZ_64G (SZ_32G * 2)
  928. #endif
  929. static __init int cxl_rch_topo_init(void)
  930. {
  931. int rc, i;
  932. for (i = 0; i < ARRAY_SIZE(cxl_rch); i++) {
  933. int idx = NR_CXL_HOST_BRIDGES + NR_CXL_SINGLE_HOST + i;
  934. struct acpi_device *adev = &host_bridge[idx];
  935. struct platform_device *pdev;
  936. pdev = platform_device_alloc("cxl_host_bridge", idx);
  937. if (!pdev)
  938. goto err_bridge;
  939. mock_companion(adev, &pdev->dev);
  940. rc = platform_device_add(pdev);
  941. if (rc) {
  942. platform_device_put(pdev);
  943. goto err_bridge;
  944. }
  945. cxl_rch[i] = pdev;
  946. mock_pci_bus[idx].bridge = &pdev->dev;
  947. rc = sysfs_create_link(&pdev->dev.kobj, &pdev->dev.kobj,
  948. "firmware_node");
  949. if (rc)
  950. goto err_bridge;
  951. }
  952. return 0;
  953. err_bridge:
  954. for (i = ARRAY_SIZE(cxl_rch) - 1; i >= 0; i--) {
  955. struct platform_device *pdev = cxl_rch[i];
  956. if (!pdev)
  957. continue;
  958. sysfs_remove_link(&pdev->dev.kobj, "firmware_node");
  959. platform_device_unregister(cxl_rch[i]);
  960. }
  961. return rc;
  962. }
  963. static void cxl_rch_topo_exit(void)
  964. {
  965. int i;
  966. for (i = ARRAY_SIZE(cxl_rch) - 1; i >= 0; i--) {
  967. struct platform_device *pdev = cxl_rch[i];
  968. if (!pdev)
  969. continue;
  970. sysfs_remove_link(&pdev->dev.kobj, "firmware_node");
  971. platform_device_unregister(cxl_rch[i]);
  972. }
  973. }
  974. static __init int cxl_single_topo_init(void)
  975. {
  976. int i, rc;
  977. for (i = 0; i < ARRAY_SIZE(cxl_hb_single); i++) {
  978. struct acpi_device *adev =
  979. &host_bridge[NR_CXL_HOST_BRIDGES + i];
  980. struct platform_device *pdev;
  981. pdev = platform_device_alloc("cxl_host_bridge",
  982. NR_CXL_HOST_BRIDGES + i);
  983. if (!pdev)
  984. goto err_bridge;
  985. mock_companion(adev, &pdev->dev);
  986. rc = platform_device_add(pdev);
  987. if (rc) {
  988. platform_device_put(pdev);
  989. goto err_bridge;
  990. }
  991. cxl_hb_single[i] = pdev;
  992. mock_pci_bus[i + NR_CXL_HOST_BRIDGES].bridge = &pdev->dev;
  993. rc = sysfs_create_link(&pdev->dev.kobj, &pdev->dev.kobj,
  994. "physical_node");
  995. if (rc)
  996. goto err_bridge;
  997. }
  998. for (i = 0; i < ARRAY_SIZE(cxl_root_single); i++) {
  999. struct platform_device *bridge =
  1000. cxl_hb_single[i % ARRAY_SIZE(cxl_hb_single)];
  1001. struct platform_device *pdev;
  1002. pdev = platform_device_alloc("cxl_root_port",
  1003. NR_MULTI_ROOT + i);
  1004. if (!pdev)
  1005. goto err_port;
  1006. pdev->dev.parent = &bridge->dev;
  1007. rc = platform_device_add(pdev);
  1008. if (rc) {
  1009. platform_device_put(pdev);
  1010. goto err_port;
  1011. }
  1012. cxl_root_single[i] = pdev;
  1013. }
  1014. for (i = 0; i < ARRAY_SIZE(cxl_swu_single); i++) {
  1015. struct platform_device *root_port = cxl_root_single[i];
  1016. struct platform_device *pdev;
  1017. pdev = platform_device_alloc("cxl_switch_uport",
  1018. NR_MULTI_ROOT + i);
  1019. if (!pdev)
  1020. goto err_uport;
  1021. pdev->dev.parent = &root_port->dev;
  1022. rc = platform_device_add(pdev);
  1023. if (rc) {
  1024. platform_device_put(pdev);
  1025. goto err_uport;
  1026. }
  1027. cxl_swu_single[i] = pdev;
  1028. }
  1029. for (i = 0; i < ARRAY_SIZE(cxl_swd_single); i++) {
  1030. struct platform_device *uport =
  1031. cxl_swu_single[i % ARRAY_SIZE(cxl_swu_single)];
  1032. struct platform_device *pdev;
  1033. pdev = platform_device_alloc("cxl_switch_dport",
  1034. i + NR_MEM_MULTI);
  1035. if (!pdev)
  1036. goto err_dport;
  1037. pdev->dev.parent = &uport->dev;
  1038. rc = platform_device_add(pdev);
  1039. if (rc) {
  1040. platform_device_put(pdev);
  1041. goto err_dport;
  1042. }
  1043. cxl_swd_single[i] = pdev;
  1044. }
  1045. return 0;
  1046. err_dport:
  1047. for (i = ARRAY_SIZE(cxl_swd_single) - 1; i >= 0; i--)
  1048. platform_device_unregister(cxl_swd_single[i]);
  1049. err_uport:
  1050. for (i = ARRAY_SIZE(cxl_swu_single) - 1; i >= 0; i--)
  1051. platform_device_unregister(cxl_swu_single[i]);
  1052. err_port:
  1053. for (i = ARRAY_SIZE(cxl_root_single) - 1; i >= 0; i--)
  1054. platform_device_unregister(cxl_root_single[i]);
  1055. err_bridge:
  1056. for (i = ARRAY_SIZE(cxl_hb_single) - 1; i >= 0; i--) {
  1057. struct platform_device *pdev = cxl_hb_single[i];
  1058. if (!pdev)
  1059. continue;
  1060. sysfs_remove_link(&pdev->dev.kobj, "physical_node");
  1061. platform_device_unregister(cxl_hb_single[i]);
  1062. }
  1063. return rc;
  1064. }
  1065. static void cxl_single_topo_exit(void)
  1066. {
  1067. int i;
  1068. for (i = ARRAY_SIZE(cxl_swd_single) - 1; i >= 0; i--)
  1069. platform_device_unregister(cxl_swd_single[i]);
  1070. for (i = ARRAY_SIZE(cxl_swu_single) - 1; i >= 0; i--)
  1071. platform_device_unregister(cxl_swu_single[i]);
  1072. for (i = ARRAY_SIZE(cxl_root_single) - 1; i >= 0; i--)
  1073. platform_device_unregister(cxl_root_single[i]);
  1074. for (i = ARRAY_SIZE(cxl_hb_single) - 1; i >= 0; i--) {
  1075. struct platform_device *pdev = cxl_hb_single[i];
  1076. if (!pdev)
  1077. continue;
  1078. sysfs_remove_link(&pdev->dev.kobj, "physical_node");
  1079. platform_device_unregister(cxl_hb_single[i]);
  1080. }
  1081. }
  1082. static void cxl_mem_exit(void)
  1083. {
  1084. int i;
  1085. for (i = ARRAY_SIZE(cxl_rcd) - 1; i >= 0; i--)
  1086. platform_device_unregister(cxl_rcd[i]);
  1087. for (i = ARRAY_SIZE(cxl_mem_single) - 1; i >= 0; i--)
  1088. platform_device_unregister(cxl_mem_single[i]);
  1089. for (i = ARRAY_SIZE(cxl_mem) - 1; i >= 0; i--)
  1090. platform_device_unregister(cxl_mem[i]);
  1091. }
  1092. static int cxl_mem_init(void)
  1093. {
  1094. int i, rc;
  1095. for (i = 0; i < ARRAY_SIZE(cxl_mem); i++) {
  1096. struct platform_device *dport = cxl_switch_dport[i];
  1097. struct platform_device *pdev;
  1098. pdev = platform_device_alloc("cxl_mem", i);
  1099. if (!pdev)
  1100. goto err_mem;
  1101. pdev->dev.parent = &dport->dev;
  1102. set_dev_node(&pdev->dev, i % 2);
  1103. rc = platform_device_add(pdev);
  1104. if (rc) {
  1105. platform_device_put(pdev);
  1106. goto err_mem;
  1107. }
  1108. cxl_mem[i] = pdev;
  1109. }
  1110. for (i = 0; i < ARRAY_SIZE(cxl_mem_single); i++) {
  1111. struct platform_device *dport = cxl_swd_single[i];
  1112. struct platform_device *pdev;
  1113. pdev = platform_device_alloc("cxl_mem", NR_MEM_MULTI + i);
  1114. if (!pdev)
  1115. goto err_single;
  1116. pdev->dev.parent = &dport->dev;
  1117. set_dev_node(&pdev->dev, i % 2);
  1118. rc = platform_device_add(pdev);
  1119. if (rc) {
  1120. platform_device_put(pdev);
  1121. goto err_single;
  1122. }
  1123. cxl_mem_single[i] = pdev;
  1124. }
  1125. for (i = 0; i < ARRAY_SIZE(cxl_rcd); i++) {
  1126. int idx = NR_MEM_MULTI + NR_MEM_SINGLE + i;
  1127. struct platform_device *rch = cxl_rch[i];
  1128. struct platform_device *pdev;
  1129. pdev = platform_device_alloc("cxl_rcd", idx);
  1130. if (!pdev)
  1131. goto err_rcd;
  1132. pdev->dev.parent = &rch->dev;
  1133. set_dev_node(&pdev->dev, i % 2);
  1134. rc = platform_device_add(pdev);
  1135. if (rc) {
  1136. platform_device_put(pdev);
  1137. goto err_rcd;
  1138. }
  1139. cxl_rcd[i] = pdev;
  1140. }
  1141. return 0;
  1142. err_rcd:
  1143. for (i = ARRAY_SIZE(cxl_rcd) - 1; i >= 0; i--)
  1144. platform_device_unregister(cxl_rcd[i]);
  1145. err_single:
  1146. for (i = ARRAY_SIZE(cxl_mem_single) - 1; i >= 0; i--)
  1147. platform_device_unregister(cxl_mem_single[i]);
  1148. err_mem:
  1149. for (i = ARRAY_SIZE(cxl_mem) - 1; i >= 0; i--)
  1150. platform_device_unregister(cxl_mem[i]);
  1151. return rc;
  1152. }
  1153. static __init int cxl_test_init(void)
  1154. {
  1155. int rc, i;
  1156. cxl_acpi_test();
  1157. cxl_core_test();
  1158. cxl_mem_test();
  1159. cxl_pmem_test();
  1160. cxl_port_test();
  1161. register_cxl_mock_ops(&cxl_mock_ops);
  1162. cxl_mock_pool = gen_pool_create(ilog2(SZ_2M), NUMA_NO_NODE);
  1163. if (!cxl_mock_pool) {
  1164. rc = -ENOMEM;
  1165. goto err_gen_pool_create;
  1166. }
  1167. rc = gen_pool_add(cxl_mock_pool, iomem_resource.end + 1 - SZ_64G,
  1168. SZ_64G, NUMA_NO_NODE);
  1169. if (rc)
  1170. goto err_gen_pool_add;
  1171. if (interleave_arithmetic == 1) {
  1172. cfmws_start = CFMWS_XOR_ARRAY_START;
  1173. cfmws_end = CFMWS_XOR_ARRAY_END;
  1174. } else {
  1175. cfmws_start = CFMWS_MOD_ARRAY_START;
  1176. cfmws_end = CFMWS_MOD_ARRAY_END;
  1177. }
  1178. rc = populate_cedt();
  1179. if (rc)
  1180. goto err_populate;
  1181. for (i = 0; i < ARRAY_SIZE(cxl_host_bridge); i++) {
  1182. struct acpi_device *adev = &host_bridge[i];
  1183. struct platform_device *pdev;
  1184. pdev = platform_device_alloc("cxl_host_bridge", i);
  1185. if (!pdev)
  1186. goto err_bridge;
  1187. mock_companion(adev, &pdev->dev);
  1188. rc = platform_device_add(pdev);
  1189. if (rc) {
  1190. platform_device_put(pdev);
  1191. goto err_bridge;
  1192. }
  1193. cxl_host_bridge[i] = pdev;
  1194. mock_pci_bus[i].bridge = &pdev->dev;
  1195. rc = sysfs_create_link(&pdev->dev.kobj, &pdev->dev.kobj,
  1196. "physical_node");
  1197. if (rc)
  1198. goto err_bridge;
  1199. }
  1200. for (i = 0; i < ARRAY_SIZE(cxl_root_port); i++) {
  1201. struct platform_device *bridge =
  1202. cxl_host_bridge[i % ARRAY_SIZE(cxl_host_bridge)];
  1203. struct platform_device *pdev;
  1204. pdev = platform_device_alloc("cxl_root_port", i);
  1205. if (!pdev)
  1206. goto err_port;
  1207. pdev->dev.parent = &bridge->dev;
  1208. rc = platform_device_add(pdev);
  1209. if (rc) {
  1210. platform_device_put(pdev);
  1211. goto err_port;
  1212. }
  1213. cxl_root_port[i] = pdev;
  1214. }
  1215. BUILD_BUG_ON(ARRAY_SIZE(cxl_switch_uport) != ARRAY_SIZE(cxl_root_port));
  1216. for (i = 0; i < ARRAY_SIZE(cxl_switch_uport); i++) {
  1217. struct platform_device *root_port = cxl_root_port[i];
  1218. struct platform_device *pdev;
  1219. pdev = platform_device_alloc("cxl_switch_uport", i);
  1220. if (!pdev)
  1221. goto err_uport;
  1222. pdev->dev.parent = &root_port->dev;
  1223. rc = platform_device_add(pdev);
  1224. if (rc) {
  1225. platform_device_put(pdev);
  1226. goto err_uport;
  1227. }
  1228. cxl_switch_uport[i] = pdev;
  1229. }
  1230. for (i = 0; i < ARRAY_SIZE(cxl_switch_dport); i++) {
  1231. struct platform_device *uport =
  1232. cxl_switch_uport[i % ARRAY_SIZE(cxl_switch_uport)];
  1233. struct platform_device *pdev;
  1234. pdev = platform_device_alloc("cxl_switch_dport", i);
  1235. if (!pdev)
  1236. goto err_dport;
  1237. pdev->dev.parent = &uport->dev;
  1238. rc = platform_device_add(pdev);
  1239. if (rc) {
  1240. platform_device_put(pdev);
  1241. goto err_dport;
  1242. }
  1243. cxl_switch_dport[i] = pdev;
  1244. }
  1245. rc = cxl_single_topo_init();
  1246. if (rc)
  1247. goto err_dport;
  1248. rc = cxl_rch_topo_init();
  1249. if (rc)
  1250. goto err_single;
  1251. cxl_acpi = platform_device_alloc("cxl_acpi", 0);
  1252. if (!cxl_acpi)
  1253. goto err_rch;
  1254. mock_companion(&acpi0017_mock, &cxl_acpi->dev);
  1255. acpi0017_mock.dev.bus = &platform_bus_type;
  1256. rc = platform_device_add(cxl_acpi);
  1257. if (rc)
  1258. goto err_root;
  1259. rc = cxl_mem_init();
  1260. if (rc)
  1261. goto err_root;
  1262. return 0;
  1263. err_root:
  1264. platform_device_put(cxl_acpi);
  1265. err_rch:
  1266. cxl_rch_topo_exit();
  1267. err_single:
  1268. cxl_single_topo_exit();
  1269. err_dport:
  1270. for (i = ARRAY_SIZE(cxl_switch_dport) - 1; i >= 0; i--)
  1271. platform_device_unregister(cxl_switch_dport[i]);
  1272. err_uport:
  1273. for (i = ARRAY_SIZE(cxl_switch_uport) - 1; i >= 0; i--)
  1274. platform_device_unregister(cxl_switch_uport[i]);
  1275. err_port:
  1276. for (i = ARRAY_SIZE(cxl_root_port) - 1; i >= 0; i--)
  1277. platform_device_unregister(cxl_root_port[i]);
  1278. err_bridge:
  1279. for (i = ARRAY_SIZE(cxl_host_bridge) - 1; i >= 0; i--) {
  1280. struct platform_device *pdev = cxl_host_bridge[i];
  1281. if (!pdev)
  1282. continue;
  1283. sysfs_remove_link(&pdev->dev.kobj, "physical_node");
  1284. platform_device_unregister(cxl_host_bridge[i]);
  1285. }
  1286. err_populate:
  1287. depopulate_all_mock_resources();
  1288. err_gen_pool_add:
  1289. gen_pool_destroy(cxl_mock_pool);
  1290. err_gen_pool_create:
  1291. unregister_cxl_mock_ops(&cxl_mock_ops);
  1292. return rc;
  1293. }
  1294. static __exit void cxl_test_exit(void)
  1295. {
  1296. int i;
  1297. cxl_mem_exit();
  1298. platform_device_unregister(cxl_acpi);
  1299. cxl_rch_topo_exit();
  1300. cxl_single_topo_exit();
  1301. for (i = ARRAY_SIZE(cxl_switch_dport) - 1; i >= 0; i--)
  1302. platform_device_unregister(cxl_switch_dport[i]);
  1303. for (i = ARRAY_SIZE(cxl_switch_uport) - 1; i >= 0; i--)
  1304. platform_device_unregister(cxl_switch_uport[i]);
  1305. for (i = ARRAY_SIZE(cxl_root_port) - 1; i >= 0; i--)
  1306. platform_device_unregister(cxl_root_port[i]);
  1307. for (i = ARRAY_SIZE(cxl_host_bridge) - 1; i >= 0; i--) {
  1308. struct platform_device *pdev = cxl_host_bridge[i];
  1309. if (!pdev)
  1310. continue;
  1311. sysfs_remove_link(&pdev->dev.kobj, "physical_node");
  1312. platform_device_unregister(cxl_host_bridge[i]);
  1313. }
  1314. depopulate_all_mock_resources();
  1315. gen_pool_destroy(cxl_mock_pool);
  1316. unregister_cxl_mock_ops(&cxl_mock_ops);
  1317. }
  1318. module_param(interleave_arithmetic, int, 0444);
  1319. MODULE_PARM_DESC(interleave_arithmetic, "Modulo:0, XOR:1");
  1320. module_init(cxl_test_init);
  1321. module_exit(cxl_test_exit);
  1322. MODULE_LICENSE("GPL v2");
  1323. MODULE_IMPORT_NS(ACPI);
  1324. MODULE_IMPORT_NS(CXL);