kfd_crat.c 37 KB

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