kfd_crat.c 40 KB

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