kfd_crat.c 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262
  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. 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_OK)
  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. int avail_size = *size;
  898. uint32_t total_num_of_cu;
  899. int num_of_cache_entries = 0;
  900. int cache_mem_filled = 0;
  901. int ret = 0;
  902. struct kfd_local_mem_info local_mem_info;
  903. if (!pcrat_image || avail_size < VCRAT_SIZE_FOR_GPU)
  904. return -EINVAL;
  905. /* Fill the CRAT Header.
  906. * Modify length and total_entries as subunits are added.
  907. */
  908. avail_size -= sizeof(struct crat_header);
  909. if (avail_size < 0)
  910. return -ENOMEM;
  911. memset(crat_table, 0, sizeof(struct crat_header));
  912. memcpy(&crat_table->signature, CRAT_SIGNATURE,
  913. sizeof(crat_table->signature));
  914. /* Change length as we add more subtypes*/
  915. crat_table->length = sizeof(struct crat_header);
  916. crat_table->num_domains = 1;
  917. crat_table->total_entries = 0;
  918. /* Fill in Subtype: Compute Unit
  919. * First fill in the sub type header and then sub type data
  920. */
  921. avail_size -= sizeof(struct crat_subtype_computeunit);
  922. if (avail_size < 0)
  923. return -ENOMEM;
  924. sub_type_hdr = (struct crat_subtype_generic *)(crat_table + 1);
  925. memset(sub_type_hdr, 0, sizeof(struct crat_subtype_computeunit));
  926. sub_type_hdr->type = CRAT_SUBTYPE_COMPUTEUNIT_AFFINITY;
  927. sub_type_hdr->length = sizeof(struct crat_subtype_computeunit);
  928. sub_type_hdr->flags = CRAT_SUBTYPE_FLAGS_ENABLED;
  929. /* Fill CU subtype data */
  930. cu = (struct crat_subtype_computeunit *)sub_type_hdr;
  931. cu->flags |= CRAT_CU_FLAGS_GPU_PRESENT;
  932. cu->proximity_domain = proximity_domain;
  933. kdev->kfd2kgd->get_cu_info(kdev->kgd, &cu_info);
  934. cu->num_simd_per_cu = cu_info.simd_per_cu;
  935. cu->num_simd_cores = cu_info.simd_per_cu * cu_info.cu_active_number;
  936. cu->max_waves_simd = cu_info.max_waves_per_simd;
  937. cu->wave_front_size = cu_info.wave_front_size;
  938. cu->array_count = cu_info.num_shader_arrays_per_engine *
  939. cu_info.num_shader_engines;
  940. total_num_of_cu = (cu->array_count * cu_info.num_cu_per_sh);
  941. cu->processor_id_low = get_and_inc_gpu_processor_id(total_num_of_cu);
  942. cu->num_cu_per_array = cu_info.num_cu_per_sh;
  943. cu->max_slots_scatch_cu = cu_info.max_scratch_slots_per_cu;
  944. cu->num_banks = cu_info.num_shader_engines;
  945. cu->lds_size_in_kb = cu_info.lds_size;
  946. cu->hsa_capability = 0;
  947. /* Check if this node supports IOMMU. During parsing this flag will
  948. * translate to HSA_CAP_ATS_PRESENT
  949. */
  950. if (!kfd_iommu_check_device(kdev))
  951. cu->hsa_capability |= CRAT_CU_FLAGS_IOMMU_PRESENT;
  952. crat_table->length += sub_type_hdr->length;
  953. crat_table->total_entries++;
  954. /* Fill in Subtype: Memory. Only on systems with large BAR (no
  955. * private FB), report memory as public. On other systems
  956. * report the total FB size (public+private) as a single
  957. * private heap.
  958. */
  959. kdev->kfd2kgd->get_local_mem_info(kdev->kgd, &local_mem_info);
  960. sub_type_hdr = (typeof(sub_type_hdr))((char *)sub_type_hdr +
  961. sub_type_hdr->length);
  962. if (debug_largebar)
  963. local_mem_info.local_mem_size_private = 0;
  964. if (local_mem_info.local_mem_size_private == 0)
  965. ret = kfd_fill_gpu_memory_affinity(&avail_size,
  966. kdev, HSA_MEM_HEAP_TYPE_FB_PUBLIC,
  967. local_mem_info.local_mem_size_public,
  968. (struct crat_subtype_memory *)sub_type_hdr,
  969. proximity_domain,
  970. &local_mem_info);
  971. else
  972. ret = kfd_fill_gpu_memory_affinity(&avail_size,
  973. kdev, HSA_MEM_HEAP_TYPE_FB_PRIVATE,
  974. local_mem_info.local_mem_size_public +
  975. local_mem_info.local_mem_size_private,
  976. (struct crat_subtype_memory *)sub_type_hdr,
  977. proximity_domain,
  978. &local_mem_info);
  979. if (ret < 0)
  980. return ret;
  981. crat_table->length += sizeof(struct crat_subtype_memory);
  982. crat_table->total_entries++;
  983. /* TODO: Fill in cache information. This information is NOT readily
  984. * available in KGD
  985. */
  986. sub_type_hdr = (typeof(sub_type_hdr))((char *)sub_type_hdr +
  987. sub_type_hdr->length);
  988. ret = kfd_fill_gpu_cache_info(kdev, cu->processor_id_low,
  989. avail_size,
  990. &cu_info,
  991. (struct crat_subtype_cache *)sub_type_hdr,
  992. &cache_mem_filled,
  993. &num_of_cache_entries);
  994. if (ret < 0)
  995. return ret;
  996. crat_table->length += cache_mem_filled;
  997. crat_table->total_entries += num_of_cache_entries;
  998. avail_size -= cache_mem_filled;
  999. /* Fill in Subtype: IO_LINKS
  1000. * Only direct links are added here which is Link from GPU to
  1001. * to its NUMA node. Indirect links are added by userspace.
  1002. */
  1003. sub_type_hdr = (typeof(sub_type_hdr))((char *)sub_type_hdr +
  1004. cache_mem_filled);
  1005. ret = kfd_fill_gpu_direct_io_link(&avail_size, kdev,
  1006. (struct crat_subtype_iolink *)sub_type_hdr, proximity_domain);
  1007. if (ret < 0)
  1008. return ret;
  1009. crat_table->length += sub_type_hdr->length;
  1010. crat_table->total_entries++;
  1011. *size = crat_table->length;
  1012. pr_info("Virtual CRAT table created for GPU\n");
  1013. return ret;
  1014. }
  1015. /* kfd_create_crat_image_virtual - Allocates memory for CRAT image and
  1016. * creates a Virtual CRAT (VCRAT) image
  1017. *
  1018. * NOTE: Call kfd_destroy_crat_image to free CRAT image memory
  1019. *
  1020. * @crat_image: VCRAT image created because ACPI does not have a
  1021. * CRAT for this device
  1022. * @size: [OUT] size of virtual crat_image
  1023. * @flags: COMPUTE_UNIT_CPU - Create VCRAT for CPU device
  1024. * COMPUTE_UNIT_GPU - Create VCRAT for GPU
  1025. * (COMPUTE_UNIT_CPU | COMPUTE_UNIT_GPU) - Create VCRAT for APU
  1026. * -- this option is not currently implemented.
  1027. * The assumption is that all AMD APUs will have CRAT
  1028. * @kdev: Valid kfd_device required if flags contain COMPUTE_UNIT_GPU
  1029. *
  1030. * Return 0 if successful else return -ve value
  1031. */
  1032. int kfd_create_crat_image_virtual(void **crat_image, size_t *size,
  1033. int flags, struct kfd_dev *kdev,
  1034. uint32_t proximity_domain)
  1035. {
  1036. void *pcrat_image = NULL;
  1037. int ret = 0;
  1038. if (!crat_image)
  1039. return -EINVAL;
  1040. *crat_image = NULL;
  1041. /* Allocate one VCRAT_SIZE_FOR_CPU for CPU virtual CRAT image and
  1042. * VCRAT_SIZE_FOR_GPU for GPU virtual CRAT image. This should cover
  1043. * all the current conditions. A check is put not to overwrite beyond
  1044. * allocated size
  1045. */
  1046. switch (flags) {
  1047. case COMPUTE_UNIT_CPU:
  1048. pcrat_image = kmalloc(VCRAT_SIZE_FOR_CPU, GFP_KERNEL);
  1049. if (!pcrat_image)
  1050. return -ENOMEM;
  1051. *size = VCRAT_SIZE_FOR_CPU;
  1052. ret = kfd_create_vcrat_image_cpu(pcrat_image, size);
  1053. break;
  1054. case COMPUTE_UNIT_GPU:
  1055. if (!kdev)
  1056. return -EINVAL;
  1057. pcrat_image = kmalloc(VCRAT_SIZE_FOR_GPU, GFP_KERNEL);
  1058. if (!pcrat_image)
  1059. return -ENOMEM;
  1060. *size = VCRAT_SIZE_FOR_GPU;
  1061. ret = kfd_create_vcrat_image_gpu(pcrat_image, size, kdev,
  1062. proximity_domain);
  1063. break;
  1064. case (COMPUTE_UNIT_CPU | COMPUTE_UNIT_GPU):
  1065. /* TODO: */
  1066. ret = -EINVAL;
  1067. pr_err("VCRAT not implemented for APU\n");
  1068. break;
  1069. default:
  1070. ret = -EINVAL;
  1071. }
  1072. if (!ret)
  1073. *crat_image = pcrat_image;
  1074. else
  1075. kfree(pcrat_image);
  1076. return ret;
  1077. }
  1078. /* kfd_destroy_crat_image
  1079. *
  1080. * @crat_image: [IN] - crat_image from kfd_create_crat_image_xxx(..)
  1081. *
  1082. */
  1083. void kfd_destroy_crat_image(void *crat_image)
  1084. {
  1085. kfree(crat_image);
  1086. }