kfd_crat.c 37 KB

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