kfd_crat.c 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308
  1. /*
  2. * Copyright 2015-2017 Advanced Micro Devices, Inc.
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  17. * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
  18. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  19. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  20. * OTHER DEALINGS IN THE SOFTWARE.
  21. */
  22. #include <linux/pci.h>
  23. #include <linux/acpi.h>
  24. #include "kfd_crat.h"
  25. #include "kfd_priv.h"
  26. #include "kfd_topology.h"
  27. #include "kfd_iommu.h"
  28. /* GPU Processor ID base for dGPUs for which VCRAT needs to be created.
  29. * GPU processor ID are expressed with Bit[31]=1.
  30. * The base is set to 0x8000_0000 + 0x1000 to avoid collision with GPU IDs
  31. * used in the CRAT.
  32. */
  33. static uint32_t gpu_processor_id_low = 0x80001000;
  34. /* Return the next available gpu_processor_id and increment it for next GPU
  35. * @total_cu_count - Total CUs present in the GPU including ones
  36. * masked off
  37. */
  38. static inline unsigned int get_and_inc_gpu_processor_id(
  39. unsigned int total_cu_count)
  40. {
  41. int current_id = gpu_processor_id_low;
  42. gpu_processor_id_low += total_cu_count;
  43. return current_id;
  44. }
  45. /* Static table to describe GPU Cache information */
  46. struct kfd_gpu_cache_info {
  47. uint32_t cache_size;
  48. uint32_t cache_level;
  49. uint32_t flags;
  50. /* Indicates how many Compute Units share this cache
  51. * Value = 1 indicates the cache is not shared
  52. */
  53. uint32_t num_cu_shared;
  54. };
  55. static struct kfd_gpu_cache_info kaveri_cache_info[] = {
  56. {
  57. /* TCP L1 Cache per CU */
  58. .cache_size = 16,
  59. .cache_level = 1,
  60. .flags = (CRAT_CACHE_FLAGS_ENABLED |
  61. CRAT_CACHE_FLAGS_DATA_CACHE |
  62. CRAT_CACHE_FLAGS_SIMD_CACHE),
  63. .num_cu_shared = 1,
  64. },
  65. {
  66. /* Scalar L1 Instruction Cache (in SQC module) per bank */
  67. .cache_size = 16,
  68. .cache_level = 1,
  69. .flags = (CRAT_CACHE_FLAGS_ENABLED |
  70. CRAT_CACHE_FLAGS_INST_CACHE |
  71. CRAT_CACHE_FLAGS_SIMD_CACHE),
  72. .num_cu_shared = 2,
  73. },
  74. {
  75. /* Scalar L1 Data Cache (in SQC module) per bank */
  76. .cache_size = 8,
  77. .cache_level = 1,
  78. .flags = (CRAT_CACHE_FLAGS_ENABLED |
  79. CRAT_CACHE_FLAGS_DATA_CACHE |
  80. CRAT_CACHE_FLAGS_SIMD_CACHE),
  81. .num_cu_shared = 2,
  82. },
  83. /* TODO: Add L2 Cache information */
  84. };
  85. static struct kfd_gpu_cache_info carrizo_cache_info[] = {
  86. {
  87. /* TCP L1 Cache per CU */
  88. .cache_size = 16,
  89. .cache_level = 1,
  90. .flags = (CRAT_CACHE_FLAGS_ENABLED |
  91. CRAT_CACHE_FLAGS_DATA_CACHE |
  92. CRAT_CACHE_FLAGS_SIMD_CACHE),
  93. .num_cu_shared = 1,
  94. },
  95. {
  96. /* Scalar L1 Instruction Cache (in SQC module) per bank */
  97. .cache_size = 8,
  98. .cache_level = 1,
  99. .flags = (CRAT_CACHE_FLAGS_ENABLED |
  100. CRAT_CACHE_FLAGS_INST_CACHE |
  101. CRAT_CACHE_FLAGS_SIMD_CACHE),
  102. .num_cu_shared = 4,
  103. },
  104. {
  105. /* Scalar L1 Data Cache (in SQC module) per bank. */
  106. .cache_size = 4,
  107. .cache_level = 1,
  108. .flags = (CRAT_CACHE_FLAGS_ENABLED |
  109. CRAT_CACHE_FLAGS_DATA_CACHE |
  110. CRAT_CACHE_FLAGS_SIMD_CACHE),
  111. .num_cu_shared = 4,
  112. },
  113. /* TODO: Add L2 Cache information */
  114. };
  115. /* NOTE: In future if more information is added to struct kfd_gpu_cache_info
  116. * the following ASICs may need a separate table.
  117. */
  118. #define hawaii_cache_info kaveri_cache_info
  119. #define tonga_cache_info carrizo_cache_info
  120. #define fiji_cache_info carrizo_cache_info
  121. #define polaris10_cache_info carrizo_cache_info
  122. #define polaris11_cache_info carrizo_cache_info
  123. /* TODO - check & update Vega10 cache details */
  124. #define vega10_cache_info carrizo_cache_info
  125. #define raven_cache_info carrizo_cache_info
  126. static void kfd_populated_cu_info_cpu(struct kfd_topology_device *dev,
  127. struct crat_subtype_computeunit *cu)
  128. {
  129. dev->node_props.cpu_cores_count = cu->num_cpu_cores;
  130. dev->node_props.cpu_core_id_base = cu->processor_id_low;
  131. if (cu->hsa_capability & CRAT_CU_FLAGS_IOMMU_PRESENT)
  132. dev->node_props.capability |= HSA_CAP_ATS_PRESENT;
  133. pr_debug("CU CPU: cores=%d id_base=%d\n", cu->num_cpu_cores,
  134. cu->processor_id_low);
  135. }
  136. static void kfd_populated_cu_info_gpu(struct kfd_topology_device *dev,
  137. struct crat_subtype_computeunit *cu)
  138. {
  139. dev->node_props.simd_id_base = cu->processor_id_low;
  140. dev->node_props.simd_count = cu->num_simd_cores;
  141. dev->node_props.lds_size_in_kb = cu->lds_size_in_kb;
  142. dev->node_props.max_waves_per_simd = cu->max_waves_simd;
  143. dev->node_props.wave_front_size = cu->wave_front_size;
  144. dev->node_props.array_count = cu->array_count;
  145. dev->node_props.cu_per_simd_array = cu->num_cu_per_array;
  146. dev->node_props.simd_per_cu = cu->num_simd_per_cu;
  147. dev->node_props.max_slots_scratch_cu = cu->max_slots_scatch_cu;
  148. if (cu->hsa_capability & CRAT_CU_FLAGS_HOT_PLUGGABLE)
  149. dev->node_props.capability |= HSA_CAP_HOT_PLUGGABLE;
  150. pr_debug("CU GPU: id_base=%d\n", cu->processor_id_low);
  151. }
  152. /* kfd_parse_subtype_cu - parse compute unit subtypes and attach it to correct
  153. * topology device present in the device_list
  154. */
  155. static int kfd_parse_subtype_cu(struct crat_subtype_computeunit *cu,
  156. struct list_head *device_list)
  157. {
  158. struct kfd_topology_device *dev;
  159. pr_debug("Found CU entry in CRAT table with proximity_domain=%d caps=%x\n",
  160. cu->proximity_domain, cu->hsa_capability);
  161. list_for_each_entry(dev, device_list, list) {
  162. if (cu->proximity_domain == dev->proximity_domain) {
  163. if (cu->flags & CRAT_CU_FLAGS_CPU_PRESENT)
  164. kfd_populated_cu_info_cpu(dev, cu);
  165. if (cu->flags & CRAT_CU_FLAGS_GPU_PRESENT)
  166. kfd_populated_cu_info_gpu(dev, cu);
  167. break;
  168. }
  169. }
  170. return 0;
  171. }
  172. static struct kfd_mem_properties *
  173. find_subtype_mem(uint32_t heap_type, uint32_t flags, uint32_t width,
  174. struct kfd_topology_device *dev)
  175. {
  176. struct kfd_mem_properties *props;
  177. list_for_each_entry(props, &dev->mem_props, list) {
  178. if (props->heap_type == heap_type
  179. && props->flags == flags
  180. && props->width == width)
  181. return props;
  182. }
  183. return NULL;
  184. }
  185. /* kfd_parse_subtype_mem - parse memory subtypes and attach it to correct
  186. * topology device present in the device_list
  187. */
  188. static int kfd_parse_subtype_mem(struct crat_subtype_memory *mem,
  189. struct list_head *device_list)
  190. {
  191. struct kfd_mem_properties *props;
  192. struct kfd_topology_device *dev;
  193. uint32_t heap_type;
  194. uint64_t size_in_bytes;
  195. uint32_t flags = 0;
  196. uint32_t width;
  197. pr_debug("Found memory entry in CRAT table with proximity_domain=%d\n",
  198. mem->proximity_domain);
  199. list_for_each_entry(dev, device_list, list) {
  200. if (mem->proximity_domain == dev->proximity_domain) {
  201. /* We're on GPU node */
  202. if (dev->node_props.cpu_cores_count == 0) {
  203. /* APU */
  204. if (mem->visibility_type == 0)
  205. heap_type =
  206. HSA_MEM_HEAP_TYPE_FB_PRIVATE;
  207. /* dGPU */
  208. else
  209. heap_type = mem->visibility_type;
  210. } else
  211. heap_type = HSA_MEM_HEAP_TYPE_SYSTEM;
  212. if (mem->flags & CRAT_MEM_FLAGS_HOT_PLUGGABLE)
  213. flags |= HSA_MEM_FLAGS_HOT_PLUGGABLE;
  214. if (mem->flags & CRAT_MEM_FLAGS_NON_VOLATILE)
  215. flags |= HSA_MEM_FLAGS_NON_VOLATILE;
  216. size_in_bytes =
  217. ((uint64_t)mem->length_high << 32) +
  218. mem->length_low;
  219. width = mem->width;
  220. /* Multiple banks of the same type are aggregated into
  221. * one. User mode doesn't care about multiple physical
  222. * memory segments. It's managed as a single virtual
  223. * heap for user mode.
  224. */
  225. props = find_subtype_mem(heap_type, flags, width, dev);
  226. if (props) {
  227. props->size_in_bytes += size_in_bytes;
  228. break;
  229. }
  230. props = kfd_alloc_struct(props);
  231. if (!props)
  232. return -ENOMEM;
  233. props->heap_type = heap_type;
  234. props->flags = flags;
  235. props->size_in_bytes = size_in_bytes;
  236. props->width = width;
  237. dev->node_props.mem_banks_count++;
  238. list_add_tail(&props->list, &dev->mem_props);
  239. break;
  240. }
  241. }
  242. return 0;
  243. }
  244. /* kfd_parse_subtype_cache - parse cache subtypes and attach it to correct
  245. * topology device present in the device_list
  246. */
  247. static int kfd_parse_subtype_cache(struct crat_subtype_cache *cache,
  248. struct list_head *device_list)
  249. {
  250. struct kfd_cache_properties *props;
  251. struct kfd_topology_device *dev;
  252. uint32_t id;
  253. uint32_t total_num_of_cu;
  254. id = cache->processor_id_low;
  255. pr_debug("Found cache entry in CRAT table with processor_id=%d\n", id);
  256. list_for_each_entry(dev, device_list, list) {
  257. total_num_of_cu = (dev->node_props.array_count *
  258. dev->node_props.cu_per_simd_array);
  259. /* Cache infomration in CRAT doesn't have proximity_domain
  260. * information as it is associated with a CPU core or GPU
  261. * Compute Unit. So map the cache using CPU core Id or SIMD
  262. * (GPU) ID.
  263. * TODO: This works because currently we can safely assume that
  264. * Compute Units are parsed before caches are parsed. In
  265. * future, remove this dependency
  266. */
  267. if ((id >= dev->node_props.cpu_core_id_base &&
  268. id <= dev->node_props.cpu_core_id_base +
  269. dev->node_props.cpu_cores_count) ||
  270. (id >= dev->node_props.simd_id_base &&
  271. id < dev->node_props.simd_id_base +
  272. total_num_of_cu)) {
  273. props = kfd_alloc_struct(props);
  274. if (!props)
  275. return -ENOMEM;
  276. props->processor_id_low = id;
  277. props->cache_level = cache->cache_level;
  278. props->cache_size = cache->cache_size;
  279. props->cacheline_size = cache->cache_line_size;
  280. props->cachelines_per_tag = cache->lines_per_tag;
  281. props->cache_assoc = cache->associativity;
  282. props->cache_latency = cache->cache_latency;
  283. memcpy(props->sibling_map, cache->sibling_map,
  284. sizeof(props->sibling_map));
  285. if (cache->flags & CRAT_CACHE_FLAGS_DATA_CACHE)
  286. props->cache_type |= HSA_CACHE_TYPE_DATA;
  287. if (cache->flags & CRAT_CACHE_FLAGS_INST_CACHE)
  288. props->cache_type |= HSA_CACHE_TYPE_INSTRUCTION;
  289. if (cache->flags & CRAT_CACHE_FLAGS_CPU_CACHE)
  290. props->cache_type |= HSA_CACHE_TYPE_CPU;
  291. if (cache->flags & CRAT_CACHE_FLAGS_SIMD_CACHE)
  292. props->cache_type |= HSA_CACHE_TYPE_HSACU;
  293. dev->cache_count++;
  294. dev->node_props.caches_count++;
  295. list_add_tail(&props->list, &dev->cache_props);
  296. break;
  297. }
  298. }
  299. return 0;
  300. }
  301. /* kfd_parse_subtype_iolink - parse iolink subtypes and attach it to correct
  302. * topology device present in the device_list
  303. */
  304. static int kfd_parse_subtype_iolink(struct crat_subtype_iolink *iolink,
  305. struct list_head *device_list)
  306. {
  307. struct kfd_iolink_properties *props = NULL, *props2;
  308. struct kfd_topology_device *dev, *cpu_dev;
  309. uint32_t id_from;
  310. uint32_t id_to;
  311. id_from = iolink->proximity_domain_from;
  312. id_to = iolink->proximity_domain_to;
  313. pr_debug("Found IO link entry in CRAT table with id_from=%d\n",
  314. id_from);
  315. list_for_each_entry(dev, device_list, list) {
  316. if (id_from == dev->proximity_domain) {
  317. props = kfd_alloc_struct(props);
  318. if (!props)
  319. return -ENOMEM;
  320. props->node_from = id_from;
  321. props->node_to = id_to;
  322. props->ver_maj = iolink->version_major;
  323. props->ver_min = iolink->version_minor;
  324. props->iolink_type = iolink->io_interface_type;
  325. if (props->iolink_type == CRAT_IOLINK_TYPE_PCIEXPRESS)
  326. props->weight = 20;
  327. else
  328. props->weight = node_distance(id_from, id_to);
  329. props->min_latency = iolink->minimum_latency;
  330. props->max_latency = iolink->maximum_latency;
  331. props->min_bandwidth = iolink->minimum_bandwidth_mbs;
  332. props->max_bandwidth = iolink->maximum_bandwidth_mbs;
  333. props->rec_transfer_size =
  334. iolink->recommended_transfer_size;
  335. dev->io_link_count++;
  336. dev->node_props.io_links_count++;
  337. list_add_tail(&props->list, &dev->io_link_props);
  338. break;
  339. }
  340. }
  341. /* CPU topology is created before GPUs are detected, so CPU->GPU
  342. * links are not built at that time. If a PCIe type is discovered, it
  343. * means a GPU is detected and we are adding GPU->CPU to the topology.
  344. * At this time, also add the corresponded CPU->GPU link.
  345. */
  346. if (props && props->iolink_type == CRAT_IOLINK_TYPE_PCIEXPRESS) {
  347. cpu_dev = kfd_topology_device_by_proximity_domain(id_to);
  348. if (!cpu_dev)
  349. return -ENODEV;
  350. /* same everything but the other direction */
  351. props2 = kmemdup(props, sizeof(*props2), GFP_KERNEL);
  352. props2->node_from = id_to;
  353. props2->node_to = id_from;
  354. props2->kobj = NULL;
  355. cpu_dev->io_link_count++;
  356. cpu_dev->node_props.io_links_count++;
  357. list_add_tail(&props2->list, &cpu_dev->io_link_props);
  358. }
  359. return 0;
  360. }
  361. /* kfd_parse_subtype - parse subtypes and attach it to correct topology device
  362. * present in the device_list
  363. * @sub_type_hdr - subtype section of crat_image
  364. * @device_list - list of topology devices present in this crat_image
  365. */
  366. static int kfd_parse_subtype(struct crat_subtype_generic *sub_type_hdr,
  367. struct list_head *device_list)
  368. {
  369. struct crat_subtype_computeunit *cu;
  370. struct crat_subtype_memory *mem;
  371. struct crat_subtype_cache *cache;
  372. struct crat_subtype_iolink *iolink;
  373. int ret = 0;
  374. switch (sub_type_hdr->type) {
  375. case CRAT_SUBTYPE_COMPUTEUNIT_AFFINITY:
  376. cu = (struct crat_subtype_computeunit *)sub_type_hdr;
  377. ret = kfd_parse_subtype_cu(cu, device_list);
  378. break;
  379. case CRAT_SUBTYPE_MEMORY_AFFINITY:
  380. mem = (struct crat_subtype_memory *)sub_type_hdr;
  381. ret = kfd_parse_subtype_mem(mem, device_list);
  382. break;
  383. case CRAT_SUBTYPE_CACHE_AFFINITY:
  384. cache = (struct crat_subtype_cache *)sub_type_hdr;
  385. ret = kfd_parse_subtype_cache(cache, device_list);
  386. break;
  387. case CRAT_SUBTYPE_TLB_AFFINITY:
  388. /*
  389. * For now, nothing to do here
  390. */
  391. pr_debug("Found TLB entry in CRAT table (not processing)\n");
  392. break;
  393. case CRAT_SUBTYPE_CCOMPUTE_AFFINITY:
  394. /*
  395. * For now, nothing to do here
  396. */
  397. pr_debug("Found CCOMPUTE entry in CRAT table (not processing)\n");
  398. break;
  399. case CRAT_SUBTYPE_IOLINK_AFFINITY:
  400. iolink = (struct crat_subtype_iolink *)sub_type_hdr;
  401. ret = kfd_parse_subtype_iolink(iolink, device_list);
  402. break;
  403. default:
  404. pr_warn("Unknown subtype %d in CRAT\n",
  405. sub_type_hdr->type);
  406. }
  407. return ret;
  408. }
  409. /* kfd_parse_crat_table - parse CRAT table. For each node present in CRAT
  410. * create a kfd_topology_device and add in to device_list. Also parse
  411. * CRAT subtypes and attach it to appropriate kfd_topology_device
  412. * @crat_image - input image containing CRAT
  413. * @device_list - [OUT] list of kfd_topology_device generated after
  414. * parsing crat_image
  415. * @proximity_domain - Proximity domain of the first device in the table
  416. *
  417. * Return - 0 if successful else -ve value
  418. */
  419. int kfd_parse_crat_table(void *crat_image, struct list_head *device_list,
  420. uint32_t proximity_domain)
  421. {
  422. struct kfd_topology_device *top_dev = NULL;
  423. struct crat_subtype_generic *sub_type_hdr;
  424. uint16_t node_id;
  425. int ret = 0;
  426. struct crat_header *crat_table = (struct crat_header *)crat_image;
  427. uint16_t num_nodes;
  428. uint32_t image_len;
  429. if (!crat_image)
  430. return -EINVAL;
  431. if (!list_empty(device_list)) {
  432. pr_warn("Error device list should be empty\n");
  433. return -EINVAL;
  434. }
  435. num_nodes = crat_table->num_domains;
  436. image_len = crat_table->length;
  437. pr_info("Parsing CRAT table with %d nodes\n", num_nodes);
  438. for (node_id = 0; node_id < num_nodes; node_id++) {
  439. top_dev = kfd_create_topology_device(device_list);
  440. if (!top_dev)
  441. break;
  442. top_dev->proximity_domain = proximity_domain++;
  443. }
  444. if (!top_dev) {
  445. ret = -ENOMEM;
  446. goto err;
  447. }
  448. memcpy(top_dev->oem_id, crat_table->oem_id, CRAT_OEMID_LENGTH);
  449. memcpy(top_dev->oem_table_id, crat_table->oem_table_id,
  450. CRAT_OEMTABLEID_LENGTH);
  451. top_dev->oem_revision = crat_table->oem_revision;
  452. sub_type_hdr = (struct crat_subtype_generic *)(crat_table+1);
  453. while ((char *)sub_type_hdr + sizeof(struct crat_subtype_generic) <
  454. ((char *)crat_image) + image_len) {
  455. if (sub_type_hdr->flags & CRAT_SUBTYPE_FLAGS_ENABLED) {
  456. ret = kfd_parse_subtype(sub_type_hdr, device_list);
  457. if (ret)
  458. break;
  459. }
  460. sub_type_hdr = (typeof(sub_type_hdr))((char *)sub_type_hdr +
  461. sub_type_hdr->length);
  462. }
  463. err:
  464. if (ret)
  465. kfd_release_topology_device_list(device_list);
  466. return ret;
  467. }
  468. /* Helper function. See kfd_fill_gpu_cache_info for parameter description */
  469. static int fill_in_pcache(struct crat_subtype_cache *pcache,
  470. struct kfd_gpu_cache_info *pcache_info,
  471. struct kfd_cu_info *cu_info,
  472. int mem_available,
  473. int cu_bitmask,
  474. int cache_type, unsigned int cu_processor_id,
  475. int cu_block)
  476. {
  477. unsigned int cu_sibling_map_mask;
  478. int first_active_cu;
  479. /* First check if enough memory is available */
  480. if (sizeof(struct crat_subtype_cache) > mem_available)
  481. return -ENOMEM;
  482. cu_sibling_map_mask = cu_bitmask;
  483. cu_sibling_map_mask >>= cu_block;
  484. cu_sibling_map_mask &=
  485. ((1 << pcache_info[cache_type].num_cu_shared) - 1);
  486. first_active_cu = ffs(cu_sibling_map_mask);
  487. /* CU could be inactive. In case of shared cache find the first active
  488. * CU. and incase of non-shared cache check if the CU is inactive. If
  489. * inactive active skip it
  490. */
  491. if (first_active_cu) {
  492. memset(pcache, 0, sizeof(struct crat_subtype_cache));
  493. pcache->type = CRAT_SUBTYPE_CACHE_AFFINITY;
  494. pcache->length = sizeof(struct crat_subtype_cache);
  495. pcache->flags = pcache_info[cache_type].flags;
  496. pcache->processor_id_low = cu_processor_id
  497. + (first_active_cu - 1);
  498. pcache->cache_level = pcache_info[cache_type].cache_level;
  499. pcache->cache_size = pcache_info[cache_type].cache_size;
  500. /* Sibling map is w.r.t processor_id_low, so shift out
  501. * inactive CU
  502. */
  503. cu_sibling_map_mask =
  504. cu_sibling_map_mask >> (first_active_cu - 1);
  505. pcache->sibling_map[0] = (uint8_t)(cu_sibling_map_mask & 0xFF);
  506. pcache->sibling_map[1] =
  507. (uint8_t)((cu_sibling_map_mask >> 8) & 0xFF);
  508. pcache->sibling_map[2] =
  509. (uint8_t)((cu_sibling_map_mask >> 16) & 0xFF);
  510. pcache->sibling_map[3] =
  511. (uint8_t)((cu_sibling_map_mask >> 24) & 0xFF);
  512. return 0;
  513. }
  514. return 1;
  515. }
  516. /* kfd_fill_gpu_cache_info - Fill GPU cache info using kfd_gpu_cache_info
  517. * tables
  518. *
  519. * @kdev - [IN] GPU device
  520. * @gpu_processor_id - [IN] GPU processor ID to which these caches
  521. * associate
  522. * @available_size - [IN] Amount of memory available in pcache
  523. * @cu_info - [IN] Compute Unit info obtained from KGD
  524. * @pcache - [OUT] memory into which cache data is to be filled in.
  525. * @size_filled - [OUT] amount of data used up in pcache.
  526. * @num_of_entries - [OUT] number of caches added
  527. */
  528. static int kfd_fill_gpu_cache_info(struct kfd_dev *kdev,
  529. int gpu_processor_id,
  530. int available_size,
  531. struct kfd_cu_info *cu_info,
  532. struct crat_subtype_cache *pcache,
  533. int *size_filled,
  534. int *num_of_entries)
  535. {
  536. struct kfd_gpu_cache_info *pcache_info;
  537. int num_of_cache_types = 0;
  538. int i, j, k;
  539. int ct = 0;
  540. int mem_available = available_size;
  541. unsigned int cu_processor_id;
  542. int ret;
  543. switch (kdev->device_info->asic_family) {
  544. case CHIP_KAVERI:
  545. pcache_info = kaveri_cache_info;
  546. num_of_cache_types = ARRAY_SIZE(kaveri_cache_info);
  547. break;
  548. case CHIP_HAWAII:
  549. pcache_info = hawaii_cache_info;
  550. num_of_cache_types = ARRAY_SIZE(hawaii_cache_info);
  551. break;
  552. case CHIP_CARRIZO:
  553. pcache_info = carrizo_cache_info;
  554. num_of_cache_types = ARRAY_SIZE(carrizo_cache_info);
  555. break;
  556. case CHIP_TONGA:
  557. pcache_info = tonga_cache_info;
  558. num_of_cache_types = ARRAY_SIZE(tonga_cache_info);
  559. break;
  560. case CHIP_FIJI:
  561. pcache_info = fiji_cache_info;
  562. num_of_cache_types = ARRAY_SIZE(fiji_cache_info);
  563. break;
  564. case CHIP_POLARIS10:
  565. pcache_info = polaris10_cache_info;
  566. num_of_cache_types = ARRAY_SIZE(polaris10_cache_info);
  567. break;
  568. case CHIP_POLARIS11:
  569. pcache_info = polaris11_cache_info;
  570. num_of_cache_types = ARRAY_SIZE(polaris11_cache_info);
  571. break;
  572. case CHIP_VEGA10:
  573. pcache_info = vega10_cache_info;
  574. num_of_cache_types = ARRAY_SIZE(vega10_cache_info);
  575. break;
  576. case CHIP_RAVEN:
  577. pcache_info = raven_cache_info;
  578. num_of_cache_types = ARRAY_SIZE(raven_cache_info);
  579. break;
  580. default:
  581. return -EINVAL;
  582. }
  583. *size_filled = 0;
  584. *num_of_entries = 0;
  585. /* For each type of cache listed in the kfd_gpu_cache_info table,
  586. * go through all available Compute Units.
  587. * The [i,j,k] loop will
  588. * if kfd_gpu_cache_info.num_cu_shared = 1
  589. * will parse through all available CU
  590. * If (kfd_gpu_cache_info.num_cu_shared != 1)
  591. * then it will consider only one CU from
  592. * the shared unit
  593. */
  594. for (ct = 0; ct < num_of_cache_types; ct++) {
  595. cu_processor_id = gpu_processor_id;
  596. for (i = 0; i < cu_info->num_shader_engines; i++) {
  597. for (j = 0; j < cu_info->num_shader_arrays_per_engine;
  598. j++) {
  599. for (k = 0; k < cu_info->num_cu_per_sh;
  600. k += pcache_info[ct].num_cu_shared) {
  601. ret = fill_in_pcache(pcache,
  602. pcache_info,
  603. cu_info,
  604. mem_available,
  605. cu_info->cu_bitmap[i][j],
  606. ct,
  607. cu_processor_id,
  608. k);
  609. if (ret < 0)
  610. break;
  611. if (!ret) {
  612. pcache++;
  613. (*num_of_entries)++;
  614. mem_available -=
  615. sizeof(*pcache);
  616. (*size_filled) +=
  617. sizeof(*pcache);
  618. }
  619. /* Move to next CU block */
  620. cu_processor_id +=
  621. pcache_info[ct].num_cu_shared;
  622. }
  623. }
  624. }
  625. }
  626. pr_debug("Added [%d] GPU cache entries\n", *num_of_entries);
  627. return 0;
  628. }
  629. /*
  630. * kfd_create_crat_image_acpi - Allocates memory for CRAT image and
  631. * copies CRAT from ACPI (if available).
  632. * NOTE: Call kfd_destroy_crat_image to free CRAT image memory
  633. *
  634. * @crat_image: CRAT read from ACPI. If no CRAT in ACPI then
  635. * crat_image will be NULL
  636. * @size: [OUT] size of crat_image
  637. *
  638. * Return 0 if successful else return error code
  639. */
  640. int kfd_create_crat_image_acpi(void **crat_image, size_t *size)
  641. {
  642. struct acpi_table_header *crat_table;
  643. acpi_status status;
  644. void *pcrat_image;
  645. if (!crat_image)
  646. return -EINVAL;
  647. *crat_image = NULL;
  648. /* Fetch the CRAT table from ACPI */
  649. status = acpi_get_table(CRAT_SIGNATURE, 0, &crat_table);
  650. if (status == AE_NOT_FOUND) {
  651. pr_warn("CRAT table not found\n");
  652. return -ENODATA;
  653. } else if (ACPI_FAILURE(status)) {
  654. const char *err = acpi_format_exception(status);
  655. pr_err("CRAT table error: %s\n", err);
  656. return -EINVAL;
  657. }
  658. if (ignore_crat) {
  659. pr_info("CRAT table disabled by module option\n");
  660. return -ENODATA;
  661. }
  662. pcrat_image = kmalloc(crat_table->length, GFP_KERNEL);
  663. if (!pcrat_image)
  664. return -ENOMEM;
  665. memcpy(pcrat_image, crat_table, crat_table->length);
  666. *crat_image = pcrat_image;
  667. *size = crat_table->length;
  668. return 0;
  669. }
  670. /* Memory required to create Virtual CRAT.
  671. * Since there is no easy way to predict the amount of memory required, the
  672. * following amount are allocated for CPU and GPU Virtual CRAT. This is
  673. * expected to cover all known conditions. But to be safe additional check
  674. * is put in the code to ensure we don't overwrite.
  675. */
  676. #define VCRAT_SIZE_FOR_CPU (2 * PAGE_SIZE)
  677. #define VCRAT_SIZE_FOR_GPU (3 * PAGE_SIZE)
  678. /* kfd_fill_cu_for_cpu - Fill in Compute info for the given CPU NUMA node
  679. *
  680. * @numa_node_id: CPU NUMA node id
  681. * @avail_size: Available size in the memory
  682. * @sub_type_hdr: Memory into which compute info will be filled in
  683. *
  684. * Return 0 if successful else return -ve value
  685. */
  686. static int kfd_fill_cu_for_cpu(int numa_node_id, int *avail_size,
  687. int proximity_domain,
  688. struct crat_subtype_computeunit *sub_type_hdr)
  689. {
  690. const struct cpumask *cpumask;
  691. *avail_size -= sizeof(struct crat_subtype_computeunit);
  692. if (*avail_size < 0)
  693. return -ENOMEM;
  694. memset(sub_type_hdr, 0, sizeof(struct crat_subtype_computeunit));
  695. /* Fill in subtype header data */
  696. sub_type_hdr->type = CRAT_SUBTYPE_COMPUTEUNIT_AFFINITY;
  697. sub_type_hdr->length = sizeof(struct crat_subtype_computeunit);
  698. sub_type_hdr->flags = CRAT_SUBTYPE_FLAGS_ENABLED;
  699. cpumask = cpumask_of_node(numa_node_id);
  700. /* Fill in CU data */
  701. sub_type_hdr->flags |= CRAT_CU_FLAGS_CPU_PRESENT;
  702. sub_type_hdr->proximity_domain = proximity_domain;
  703. sub_type_hdr->processor_id_low = kfd_numa_node_to_apic_id(numa_node_id);
  704. if (sub_type_hdr->processor_id_low == -1)
  705. return -EINVAL;
  706. sub_type_hdr->num_cpu_cores = cpumask_weight(cpumask);
  707. return 0;
  708. }
  709. /* kfd_fill_mem_info_for_cpu - Fill in Memory info for the given CPU NUMA node
  710. *
  711. * @numa_node_id: CPU NUMA node id
  712. * @avail_size: Available size in the memory
  713. * @sub_type_hdr: Memory into which compute info will be filled in
  714. *
  715. * Return 0 if successful else return -ve value
  716. */
  717. static int kfd_fill_mem_info_for_cpu(int numa_node_id, int *avail_size,
  718. int proximity_domain,
  719. struct crat_subtype_memory *sub_type_hdr)
  720. {
  721. uint64_t mem_in_bytes = 0;
  722. pg_data_t *pgdat;
  723. int zone_type;
  724. *avail_size -= sizeof(struct crat_subtype_memory);
  725. if (*avail_size < 0)
  726. return -ENOMEM;
  727. memset(sub_type_hdr, 0, sizeof(struct crat_subtype_memory));
  728. /* Fill in subtype header data */
  729. sub_type_hdr->type = CRAT_SUBTYPE_MEMORY_AFFINITY;
  730. sub_type_hdr->length = sizeof(struct crat_subtype_memory);
  731. sub_type_hdr->flags = CRAT_SUBTYPE_FLAGS_ENABLED;
  732. /* Fill in Memory Subunit data */
  733. /* Unlike si_meminfo, si_meminfo_node is not exported. So
  734. * the following lines are duplicated from si_meminfo_node
  735. * function
  736. */
  737. pgdat = NODE_DATA(numa_node_id);
  738. for (zone_type = 0; zone_type < MAX_NR_ZONES; zone_type++)
  739. mem_in_bytes += pgdat->node_zones[zone_type].managed_pages;
  740. mem_in_bytes <<= PAGE_SHIFT;
  741. sub_type_hdr->length_low = lower_32_bits(mem_in_bytes);
  742. sub_type_hdr->length_high = upper_32_bits(mem_in_bytes);
  743. sub_type_hdr->proximity_domain = proximity_domain;
  744. return 0;
  745. }
  746. static int kfd_fill_iolink_info_for_cpu(int numa_node_id, int *avail_size,
  747. uint32_t *num_entries,
  748. struct crat_subtype_iolink *sub_type_hdr)
  749. {
  750. int nid;
  751. struct cpuinfo_x86 *c = &cpu_data(0);
  752. uint8_t link_type;
  753. if (c->x86_vendor == X86_VENDOR_AMD)
  754. link_type = CRAT_IOLINK_TYPE_HYPERTRANSPORT;
  755. else
  756. link_type = CRAT_IOLINK_TYPE_QPI_1_1;
  757. *num_entries = 0;
  758. /* Create IO links from this node to other CPU nodes */
  759. for_each_online_node(nid) {
  760. if (nid == numa_node_id) /* node itself */
  761. continue;
  762. *avail_size -= sizeof(struct crat_subtype_iolink);
  763. if (*avail_size < 0)
  764. return -ENOMEM;
  765. memset(sub_type_hdr, 0, sizeof(struct crat_subtype_iolink));
  766. /* Fill in subtype header data */
  767. sub_type_hdr->type = CRAT_SUBTYPE_IOLINK_AFFINITY;
  768. sub_type_hdr->length = sizeof(struct crat_subtype_iolink);
  769. sub_type_hdr->flags = CRAT_SUBTYPE_FLAGS_ENABLED;
  770. /* Fill in IO link data */
  771. sub_type_hdr->proximity_domain_from = numa_node_id;
  772. sub_type_hdr->proximity_domain_to = nid;
  773. sub_type_hdr->io_interface_type = link_type;
  774. (*num_entries)++;
  775. sub_type_hdr++;
  776. }
  777. return 0;
  778. }
  779. /* kfd_create_vcrat_image_cpu - Create Virtual CRAT for CPU
  780. *
  781. * @pcrat_image: Fill in VCRAT for CPU
  782. * @size: [IN] allocated size of crat_image.
  783. * [OUT] actual size of data filled in crat_image
  784. */
  785. static int kfd_create_vcrat_image_cpu(void *pcrat_image, size_t *size)
  786. {
  787. struct crat_header *crat_table = (struct crat_header *)pcrat_image;
  788. struct acpi_table_header *acpi_table;
  789. acpi_status status;
  790. struct crat_subtype_generic *sub_type_hdr;
  791. int avail_size = *size;
  792. int numa_node_id;
  793. uint32_t entries = 0;
  794. int ret = 0;
  795. if (!pcrat_image || avail_size < VCRAT_SIZE_FOR_CPU)
  796. return -EINVAL;
  797. /* Fill in CRAT Header.
  798. * Modify length and total_entries as subunits are added.
  799. */
  800. avail_size -= sizeof(struct crat_header);
  801. if (avail_size < 0)
  802. return -ENOMEM;
  803. memset(crat_table, 0, sizeof(struct crat_header));
  804. memcpy(&crat_table->signature, CRAT_SIGNATURE,
  805. sizeof(crat_table->signature));
  806. crat_table->length = sizeof(struct crat_header);
  807. status = acpi_get_table("DSDT", 0, &acpi_table);
  808. if (status != AE_OK)
  809. pr_warn("DSDT table not found for OEM information\n");
  810. else {
  811. crat_table->oem_revision = acpi_table->revision;
  812. memcpy(crat_table->oem_id, acpi_table->oem_id,
  813. CRAT_OEMID_LENGTH);
  814. memcpy(crat_table->oem_table_id, acpi_table->oem_table_id,
  815. CRAT_OEMTABLEID_LENGTH);
  816. }
  817. crat_table->total_entries = 0;
  818. crat_table->num_domains = 0;
  819. sub_type_hdr = (struct crat_subtype_generic *)(crat_table+1);
  820. for_each_online_node(numa_node_id) {
  821. if (kfd_numa_node_to_apic_id(numa_node_id) == -1)
  822. continue;
  823. /* Fill in Subtype: Compute Unit */
  824. ret = kfd_fill_cu_for_cpu(numa_node_id, &avail_size,
  825. crat_table->num_domains,
  826. (struct crat_subtype_computeunit *)sub_type_hdr);
  827. if (ret < 0)
  828. return ret;
  829. crat_table->length += sub_type_hdr->length;
  830. crat_table->total_entries++;
  831. sub_type_hdr = (typeof(sub_type_hdr))((char *)sub_type_hdr +
  832. sub_type_hdr->length);
  833. /* Fill in Subtype: Memory */
  834. ret = kfd_fill_mem_info_for_cpu(numa_node_id, &avail_size,
  835. crat_table->num_domains,
  836. (struct crat_subtype_memory *)sub_type_hdr);
  837. if (ret < 0)
  838. return ret;
  839. crat_table->length += sub_type_hdr->length;
  840. crat_table->total_entries++;
  841. sub_type_hdr = (typeof(sub_type_hdr))((char *)sub_type_hdr +
  842. sub_type_hdr->length);
  843. /* Fill in Subtype: IO Link */
  844. ret = kfd_fill_iolink_info_for_cpu(numa_node_id, &avail_size,
  845. &entries,
  846. (struct crat_subtype_iolink *)sub_type_hdr);
  847. if (ret < 0)
  848. return ret;
  849. crat_table->length += (sub_type_hdr->length * entries);
  850. crat_table->total_entries += entries;
  851. sub_type_hdr = (typeof(sub_type_hdr))((char *)sub_type_hdr +
  852. sub_type_hdr->length * entries);
  853. crat_table->num_domains++;
  854. }
  855. /* TODO: Add cache Subtype for CPU.
  856. * Currently, CPU cache information is available in function
  857. * detect_cache_attributes(cpu) defined in the file
  858. * ./arch/x86/kernel/cpu/intel_cacheinfo.c. This function is not
  859. * exported and to get the same information the code needs to be
  860. * duplicated.
  861. */
  862. *size = crat_table->length;
  863. pr_info("Virtual CRAT table created for CPU\n");
  864. return 0;
  865. }
  866. static int kfd_fill_gpu_memory_affinity(int *avail_size,
  867. struct kfd_dev *kdev, uint8_t type, uint64_t size,
  868. struct crat_subtype_memory *sub_type_hdr,
  869. uint32_t proximity_domain,
  870. const struct kfd_local_mem_info *local_mem_info)
  871. {
  872. *avail_size -= sizeof(struct crat_subtype_memory);
  873. if (*avail_size < 0)
  874. return -ENOMEM;
  875. memset((void *)sub_type_hdr, 0, sizeof(struct crat_subtype_memory));
  876. sub_type_hdr->type = CRAT_SUBTYPE_MEMORY_AFFINITY;
  877. sub_type_hdr->length = sizeof(struct crat_subtype_memory);
  878. sub_type_hdr->flags |= CRAT_SUBTYPE_FLAGS_ENABLED;
  879. sub_type_hdr->proximity_domain = proximity_domain;
  880. pr_debug("Fill gpu memory affinity - type 0x%x size 0x%llx\n",
  881. type, size);
  882. sub_type_hdr->length_low = lower_32_bits(size);
  883. sub_type_hdr->length_high = upper_32_bits(size);
  884. sub_type_hdr->width = local_mem_info->vram_width;
  885. sub_type_hdr->visibility_type = type;
  886. return 0;
  887. }
  888. /* kfd_fill_gpu_direct_io_link - Fill in direct io link from GPU
  889. * to its NUMA node
  890. * @avail_size: Available size in the memory
  891. * @kdev - [IN] GPU device
  892. * @sub_type_hdr: Memory into which io link info will be filled in
  893. * @proximity_domain - proximity domain of the GPU node
  894. *
  895. * Return 0 if successful else return -ve value
  896. */
  897. static int kfd_fill_gpu_direct_io_link(int *avail_size,
  898. struct kfd_dev *kdev,
  899. struct crat_subtype_iolink *sub_type_hdr,
  900. uint32_t proximity_domain)
  901. {
  902. *avail_size -= sizeof(struct crat_subtype_iolink);
  903. if (*avail_size < 0)
  904. return -ENOMEM;
  905. memset((void *)sub_type_hdr, 0, sizeof(struct crat_subtype_iolink));
  906. /* Fill in subtype header data */
  907. sub_type_hdr->type = CRAT_SUBTYPE_IOLINK_AFFINITY;
  908. sub_type_hdr->length = sizeof(struct crat_subtype_iolink);
  909. sub_type_hdr->flags |= CRAT_SUBTYPE_FLAGS_ENABLED;
  910. /* Fill in IOLINK subtype.
  911. * TODO: Fill-in other fields of iolink subtype
  912. */
  913. sub_type_hdr->io_interface_type = CRAT_IOLINK_TYPE_PCIEXPRESS;
  914. sub_type_hdr->proximity_domain_from = proximity_domain;
  915. #ifdef CONFIG_NUMA
  916. if (kdev->pdev->dev.numa_node == NUMA_NO_NODE)
  917. sub_type_hdr->proximity_domain_to = 0;
  918. else
  919. sub_type_hdr->proximity_domain_to = kdev->pdev->dev.numa_node;
  920. #else
  921. sub_type_hdr->proximity_domain_to = 0;
  922. #endif
  923. return 0;
  924. }
  925. /* kfd_create_vcrat_image_gpu - Create Virtual CRAT for CPU
  926. *
  927. * @pcrat_image: Fill in VCRAT for GPU
  928. * @size: [IN] allocated size of crat_image.
  929. * [OUT] actual size of data filled in crat_image
  930. */
  931. static int kfd_create_vcrat_image_gpu(void *pcrat_image,
  932. size_t *size, struct kfd_dev *kdev,
  933. uint32_t proximity_domain)
  934. {
  935. struct crat_header *crat_table = (struct crat_header *)pcrat_image;
  936. struct crat_subtype_generic *sub_type_hdr;
  937. struct crat_subtype_computeunit *cu;
  938. struct kfd_cu_info cu_info;
  939. int avail_size = *size;
  940. uint32_t total_num_of_cu;
  941. int num_of_cache_entries = 0;
  942. int cache_mem_filled = 0;
  943. int ret = 0;
  944. struct kfd_local_mem_info local_mem_info;
  945. if (!pcrat_image || avail_size < VCRAT_SIZE_FOR_GPU)
  946. return -EINVAL;
  947. /* Fill the CRAT Header.
  948. * Modify length and total_entries as subunits are added.
  949. */
  950. avail_size -= sizeof(struct crat_header);
  951. if (avail_size < 0)
  952. return -ENOMEM;
  953. memset(crat_table, 0, sizeof(struct crat_header));
  954. memcpy(&crat_table->signature, CRAT_SIGNATURE,
  955. sizeof(crat_table->signature));
  956. /* Change length as we add more subtypes*/
  957. crat_table->length = sizeof(struct crat_header);
  958. crat_table->num_domains = 1;
  959. crat_table->total_entries = 0;
  960. /* Fill in Subtype: Compute Unit
  961. * First fill in the sub type header and then sub type data
  962. */
  963. avail_size -= sizeof(struct crat_subtype_computeunit);
  964. if (avail_size < 0)
  965. return -ENOMEM;
  966. sub_type_hdr = (struct crat_subtype_generic *)(crat_table + 1);
  967. memset(sub_type_hdr, 0, sizeof(struct crat_subtype_computeunit));
  968. sub_type_hdr->type = CRAT_SUBTYPE_COMPUTEUNIT_AFFINITY;
  969. sub_type_hdr->length = sizeof(struct crat_subtype_computeunit);
  970. sub_type_hdr->flags = CRAT_SUBTYPE_FLAGS_ENABLED;
  971. /* Fill CU subtype data */
  972. cu = (struct crat_subtype_computeunit *)sub_type_hdr;
  973. cu->flags |= CRAT_CU_FLAGS_GPU_PRESENT;
  974. cu->proximity_domain = proximity_domain;
  975. kdev->kfd2kgd->get_cu_info(kdev->kgd, &cu_info);
  976. cu->num_simd_per_cu = cu_info.simd_per_cu;
  977. cu->num_simd_cores = cu_info.simd_per_cu * cu_info.cu_active_number;
  978. cu->max_waves_simd = cu_info.max_waves_per_simd;
  979. cu->wave_front_size = cu_info.wave_front_size;
  980. cu->array_count = cu_info.num_shader_arrays_per_engine *
  981. cu_info.num_shader_engines;
  982. total_num_of_cu = (cu->array_count * cu_info.num_cu_per_sh);
  983. cu->processor_id_low = get_and_inc_gpu_processor_id(total_num_of_cu);
  984. cu->num_cu_per_array = cu_info.num_cu_per_sh;
  985. cu->max_slots_scatch_cu = cu_info.max_scratch_slots_per_cu;
  986. cu->num_banks = cu_info.num_shader_engines;
  987. cu->lds_size_in_kb = cu_info.lds_size;
  988. cu->hsa_capability = 0;
  989. /* Check if this node supports IOMMU. During parsing this flag will
  990. * translate to HSA_CAP_ATS_PRESENT
  991. */
  992. if (!kfd_iommu_check_device(kdev))
  993. cu->hsa_capability |= CRAT_CU_FLAGS_IOMMU_PRESENT;
  994. crat_table->length += sub_type_hdr->length;
  995. crat_table->total_entries++;
  996. /* Fill in Subtype: Memory. Only on systems with large BAR (no
  997. * private FB), report memory as public. On other systems
  998. * report the total FB size (public+private) as a single
  999. * private heap.
  1000. */
  1001. kdev->kfd2kgd->get_local_mem_info(kdev->kgd, &local_mem_info);
  1002. sub_type_hdr = (typeof(sub_type_hdr))((char *)sub_type_hdr +
  1003. sub_type_hdr->length);
  1004. if (debug_largebar)
  1005. local_mem_info.local_mem_size_private = 0;
  1006. if (local_mem_info.local_mem_size_private == 0)
  1007. ret = kfd_fill_gpu_memory_affinity(&avail_size,
  1008. kdev, HSA_MEM_HEAP_TYPE_FB_PUBLIC,
  1009. local_mem_info.local_mem_size_public,
  1010. (struct crat_subtype_memory *)sub_type_hdr,
  1011. proximity_domain,
  1012. &local_mem_info);
  1013. else
  1014. ret = kfd_fill_gpu_memory_affinity(&avail_size,
  1015. kdev, HSA_MEM_HEAP_TYPE_FB_PRIVATE,
  1016. local_mem_info.local_mem_size_public +
  1017. local_mem_info.local_mem_size_private,
  1018. (struct crat_subtype_memory *)sub_type_hdr,
  1019. proximity_domain,
  1020. &local_mem_info);
  1021. if (ret < 0)
  1022. return ret;
  1023. crat_table->length += sizeof(struct crat_subtype_memory);
  1024. crat_table->total_entries++;
  1025. /* TODO: Fill in cache information. This information is NOT readily
  1026. * available in KGD
  1027. */
  1028. sub_type_hdr = (typeof(sub_type_hdr))((char *)sub_type_hdr +
  1029. sub_type_hdr->length);
  1030. ret = kfd_fill_gpu_cache_info(kdev, cu->processor_id_low,
  1031. avail_size,
  1032. &cu_info,
  1033. (struct crat_subtype_cache *)sub_type_hdr,
  1034. &cache_mem_filled,
  1035. &num_of_cache_entries);
  1036. if (ret < 0)
  1037. return ret;
  1038. crat_table->length += cache_mem_filled;
  1039. crat_table->total_entries += num_of_cache_entries;
  1040. avail_size -= cache_mem_filled;
  1041. /* Fill in Subtype: IO_LINKS
  1042. * Only direct links are added here which is Link from GPU to
  1043. * to its NUMA node. Indirect links are added by userspace.
  1044. */
  1045. sub_type_hdr = (typeof(sub_type_hdr))((char *)sub_type_hdr +
  1046. cache_mem_filled);
  1047. ret = kfd_fill_gpu_direct_io_link(&avail_size, kdev,
  1048. (struct crat_subtype_iolink *)sub_type_hdr, proximity_domain);
  1049. if (ret < 0)
  1050. return ret;
  1051. crat_table->length += sub_type_hdr->length;
  1052. crat_table->total_entries++;
  1053. *size = crat_table->length;
  1054. pr_info("Virtual CRAT table created for GPU\n");
  1055. return ret;
  1056. }
  1057. /* kfd_create_crat_image_virtual - Allocates memory for CRAT image and
  1058. * creates a Virtual CRAT (VCRAT) image
  1059. *
  1060. * NOTE: Call kfd_destroy_crat_image to free CRAT image memory
  1061. *
  1062. * @crat_image: VCRAT image created because ACPI does not have a
  1063. * CRAT for this device
  1064. * @size: [OUT] size of virtual crat_image
  1065. * @flags: COMPUTE_UNIT_CPU - Create VCRAT for CPU device
  1066. * COMPUTE_UNIT_GPU - Create VCRAT for GPU
  1067. * (COMPUTE_UNIT_CPU | COMPUTE_UNIT_GPU) - Create VCRAT for APU
  1068. * -- this option is not currently implemented.
  1069. * The assumption is that all AMD APUs will have CRAT
  1070. * @kdev: Valid kfd_device required if flags contain COMPUTE_UNIT_GPU
  1071. *
  1072. * Return 0 if successful else return -ve value
  1073. */
  1074. int kfd_create_crat_image_virtual(void **crat_image, size_t *size,
  1075. int flags, struct kfd_dev *kdev,
  1076. uint32_t proximity_domain)
  1077. {
  1078. void *pcrat_image = NULL;
  1079. int ret = 0;
  1080. if (!crat_image)
  1081. return -EINVAL;
  1082. *crat_image = NULL;
  1083. /* Allocate one VCRAT_SIZE_FOR_CPU for CPU virtual CRAT image and
  1084. * VCRAT_SIZE_FOR_GPU for GPU virtual CRAT image. This should cover
  1085. * all the current conditions. A check is put not to overwrite beyond
  1086. * allocated size
  1087. */
  1088. switch (flags) {
  1089. case COMPUTE_UNIT_CPU:
  1090. pcrat_image = kmalloc(VCRAT_SIZE_FOR_CPU, GFP_KERNEL);
  1091. if (!pcrat_image)
  1092. return -ENOMEM;
  1093. *size = VCRAT_SIZE_FOR_CPU;
  1094. ret = kfd_create_vcrat_image_cpu(pcrat_image, size);
  1095. break;
  1096. case COMPUTE_UNIT_GPU:
  1097. if (!kdev)
  1098. return -EINVAL;
  1099. pcrat_image = kmalloc(VCRAT_SIZE_FOR_GPU, GFP_KERNEL);
  1100. if (!pcrat_image)
  1101. return -ENOMEM;
  1102. *size = VCRAT_SIZE_FOR_GPU;
  1103. ret = kfd_create_vcrat_image_gpu(pcrat_image, size, kdev,
  1104. proximity_domain);
  1105. break;
  1106. case (COMPUTE_UNIT_CPU | COMPUTE_UNIT_GPU):
  1107. /* TODO: */
  1108. ret = -EINVAL;
  1109. pr_err("VCRAT not implemented for APU\n");
  1110. break;
  1111. default:
  1112. ret = -EINVAL;
  1113. }
  1114. if (!ret)
  1115. *crat_image = pcrat_image;
  1116. else
  1117. kfree(pcrat_image);
  1118. return ret;
  1119. }
  1120. /* kfd_destroy_crat_image
  1121. *
  1122. * @crat_image: [IN] - crat_image from kfd_create_crat_image_xxx(..)
  1123. *
  1124. */
  1125. void kfd_destroy_crat_image(void *crat_image)
  1126. {
  1127. kfree(crat_image);
  1128. }