kfd_crat.c 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309
  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. }