kfd_topology.c 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235
  1. /*
  2. * Copyright 2014 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/types.h>
  23. #include <linux/kernel.h>
  24. #include <linux/pci.h>
  25. #include <linux/errno.h>
  26. #include <linux/acpi.h>
  27. #include <linux/hash.h>
  28. #include <linux/cpufreq.h>
  29. #include <linux/log2.h>
  30. #include "kfd_priv.h"
  31. #include "kfd_crat.h"
  32. #include "kfd_topology.h"
  33. static struct list_head topology_device_list;
  34. static int topology_crat_parsed;
  35. static struct kfd_system_properties sys_props;
  36. static DECLARE_RWSEM(topology_lock);
  37. struct kfd_dev *kfd_device_by_id(uint32_t gpu_id)
  38. {
  39. struct kfd_topology_device *top_dev;
  40. struct kfd_dev *device = NULL;
  41. down_read(&topology_lock);
  42. list_for_each_entry(top_dev, &topology_device_list, list)
  43. if (top_dev->gpu_id == gpu_id) {
  44. device = top_dev->gpu;
  45. break;
  46. }
  47. up_read(&topology_lock);
  48. return device;
  49. }
  50. struct kfd_dev *kfd_device_by_pci_dev(const struct pci_dev *pdev)
  51. {
  52. struct kfd_topology_device *top_dev;
  53. struct kfd_dev *device = NULL;
  54. down_read(&topology_lock);
  55. list_for_each_entry(top_dev, &topology_device_list, list)
  56. if (top_dev->gpu->pdev == pdev) {
  57. device = top_dev->gpu;
  58. break;
  59. }
  60. up_read(&topology_lock);
  61. return device;
  62. }
  63. static int kfd_topology_get_crat_acpi(void *crat_image, size_t *size)
  64. {
  65. struct acpi_table_header *crat_table;
  66. acpi_status status;
  67. if (!size)
  68. return -EINVAL;
  69. /*
  70. * Fetch the CRAT table from ACPI
  71. */
  72. status = acpi_get_table(CRAT_SIGNATURE, 0, &crat_table);
  73. if (status == AE_NOT_FOUND) {
  74. pr_warn("CRAT table not found\n");
  75. return -ENODATA;
  76. } else if (ACPI_FAILURE(status)) {
  77. const char *err = acpi_format_exception(status);
  78. pr_err("CRAT table error: %s\n", err);
  79. return -EINVAL;
  80. }
  81. if (*size >= crat_table->length && crat_image != NULL)
  82. memcpy(crat_image, crat_table, crat_table->length);
  83. *size = crat_table->length;
  84. return 0;
  85. }
  86. static void kfd_populated_cu_info_cpu(struct kfd_topology_device *dev,
  87. struct crat_subtype_computeunit *cu)
  88. {
  89. dev->node_props.cpu_cores_count = cu->num_cpu_cores;
  90. dev->node_props.cpu_core_id_base = cu->processor_id_low;
  91. if (cu->hsa_capability & CRAT_CU_FLAGS_IOMMU_PRESENT)
  92. dev->node_props.capability |= HSA_CAP_ATS_PRESENT;
  93. pr_info("CU CPU: cores=%d id_base=%d\n", cu->num_cpu_cores,
  94. cu->processor_id_low);
  95. }
  96. static void kfd_populated_cu_info_gpu(struct kfd_topology_device *dev,
  97. struct crat_subtype_computeunit *cu)
  98. {
  99. dev->node_props.simd_id_base = cu->processor_id_low;
  100. dev->node_props.simd_count = cu->num_simd_cores;
  101. dev->node_props.lds_size_in_kb = cu->lds_size_in_kb;
  102. dev->node_props.max_waves_per_simd = cu->max_waves_simd;
  103. dev->node_props.wave_front_size = cu->wave_front_size;
  104. dev->node_props.mem_banks_count = cu->num_banks;
  105. dev->node_props.array_count = cu->num_arrays;
  106. dev->node_props.cu_per_simd_array = cu->num_cu_per_array;
  107. dev->node_props.simd_per_cu = cu->num_simd_per_cu;
  108. dev->node_props.max_slots_scratch_cu = cu->max_slots_scatch_cu;
  109. if (cu->hsa_capability & CRAT_CU_FLAGS_HOT_PLUGGABLE)
  110. dev->node_props.capability |= HSA_CAP_HOT_PLUGGABLE;
  111. pr_info("CU GPU: simds=%d id_base=%d\n", cu->num_simd_cores,
  112. cu->processor_id_low);
  113. }
  114. /* kfd_parse_subtype_cu is called when the topology mutex is already acquired */
  115. static int kfd_parse_subtype_cu(struct crat_subtype_computeunit *cu)
  116. {
  117. struct kfd_topology_device *dev;
  118. int i = 0;
  119. pr_info("Found CU entry in CRAT table with proximity_domain=%d caps=%x\n",
  120. cu->proximity_domain, cu->hsa_capability);
  121. list_for_each_entry(dev, &topology_device_list, list) {
  122. if (cu->proximity_domain == i) {
  123. if (cu->flags & CRAT_CU_FLAGS_CPU_PRESENT)
  124. kfd_populated_cu_info_cpu(dev, cu);
  125. if (cu->flags & CRAT_CU_FLAGS_GPU_PRESENT)
  126. kfd_populated_cu_info_gpu(dev, cu);
  127. break;
  128. }
  129. i++;
  130. }
  131. return 0;
  132. }
  133. /*
  134. * kfd_parse_subtype_mem is called when the topology mutex is
  135. * already acquired
  136. */
  137. static int kfd_parse_subtype_mem(struct crat_subtype_memory *mem)
  138. {
  139. struct kfd_mem_properties *props;
  140. struct kfd_topology_device *dev;
  141. int i = 0;
  142. pr_info("Found memory entry in CRAT table with proximity_domain=%d\n",
  143. mem->promixity_domain);
  144. list_for_each_entry(dev, &topology_device_list, list) {
  145. if (mem->promixity_domain == i) {
  146. props = kfd_alloc_struct(props);
  147. if (props == NULL)
  148. return -ENOMEM;
  149. if (dev->node_props.cpu_cores_count == 0)
  150. props->heap_type = HSA_MEM_HEAP_TYPE_FB_PRIVATE;
  151. else
  152. props->heap_type = HSA_MEM_HEAP_TYPE_SYSTEM;
  153. if (mem->flags & CRAT_MEM_FLAGS_HOT_PLUGGABLE)
  154. props->flags |= HSA_MEM_FLAGS_HOT_PLUGGABLE;
  155. if (mem->flags & CRAT_MEM_FLAGS_NON_VOLATILE)
  156. props->flags |= HSA_MEM_FLAGS_NON_VOLATILE;
  157. props->size_in_bytes =
  158. ((uint64_t)mem->length_high << 32) +
  159. mem->length_low;
  160. props->width = mem->width;
  161. dev->mem_bank_count++;
  162. list_add_tail(&props->list, &dev->mem_props);
  163. break;
  164. }
  165. i++;
  166. }
  167. return 0;
  168. }
  169. /*
  170. * kfd_parse_subtype_cache is called when the topology mutex
  171. * is already acquired
  172. */
  173. static int kfd_parse_subtype_cache(struct crat_subtype_cache *cache)
  174. {
  175. struct kfd_cache_properties *props;
  176. struct kfd_topology_device *dev;
  177. uint32_t id;
  178. id = cache->processor_id_low;
  179. pr_info("Found cache entry in CRAT table with processor_id=%d\n", id);
  180. list_for_each_entry(dev, &topology_device_list, list)
  181. if (id == dev->node_props.cpu_core_id_base ||
  182. id == dev->node_props.simd_id_base) {
  183. props = kfd_alloc_struct(props);
  184. if (props == NULL)
  185. return -ENOMEM;
  186. props->processor_id_low = id;
  187. props->cache_level = cache->cache_level;
  188. props->cache_size = cache->cache_size;
  189. props->cacheline_size = cache->cache_line_size;
  190. props->cachelines_per_tag = cache->lines_per_tag;
  191. props->cache_assoc = cache->associativity;
  192. props->cache_latency = cache->cache_latency;
  193. if (cache->flags & CRAT_CACHE_FLAGS_DATA_CACHE)
  194. props->cache_type |= HSA_CACHE_TYPE_DATA;
  195. if (cache->flags & CRAT_CACHE_FLAGS_INST_CACHE)
  196. props->cache_type |= HSA_CACHE_TYPE_INSTRUCTION;
  197. if (cache->flags & CRAT_CACHE_FLAGS_CPU_CACHE)
  198. props->cache_type |= HSA_CACHE_TYPE_CPU;
  199. if (cache->flags & CRAT_CACHE_FLAGS_SIMD_CACHE)
  200. props->cache_type |= HSA_CACHE_TYPE_HSACU;
  201. dev->cache_count++;
  202. dev->node_props.caches_count++;
  203. list_add_tail(&props->list, &dev->cache_props);
  204. break;
  205. }
  206. return 0;
  207. }
  208. /*
  209. * kfd_parse_subtype_iolink is called when the topology mutex
  210. * is already acquired
  211. */
  212. static int kfd_parse_subtype_iolink(struct crat_subtype_iolink *iolink)
  213. {
  214. struct kfd_iolink_properties *props;
  215. struct kfd_topology_device *dev;
  216. uint32_t i = 0;
  217. uint32_t id_from;
  218. uint32_t id_to;
  219. id_from = iolink->proximity_domain_from;
  220. id_to = iolink->proximity_domain_to;
  221. pr_info("Found IO link entry in CRAT table with id_from=%d\n", id_from);
  222. list_for_each_entry(dev, &topology_device_list, list) {
  223. if (id_from == i) {
  224. props = kfd_alloc_struct(props);
  225. if (props == NULL)
  226. return -ENOMEM;
  227. props->node_from = id_from;
  228. props->node_to = id_to;
  229. props->ver_maj = iolink->version_major;
  230. props->ver_min = iolink->version_minor;
  231. /*
  232. * weight factor (derived from CDIR), currently always 1
  233. */
  234. props->weight = 1;
  235. props->min_latency = iolink->minimum_latency;
  236. props->max_latency = iolink->maximum_latency;
  237. props->min_bandwidth = iolink->minimum_bandwidth_mbs;
  238. props->max_bandwidth = iolink->maximum_bandwidth_mbs;
  239. props->rec_transfer_size =
  240. iolink->recommended_transfer_size;
  241. dev->io_link_count++;
  242. dev->node_props.io_links_count++;
  243. list_add_tail(&props->list, &dev->io_link_props);
  244. break;
  245. }
  246. i++;
  247. }
  248. return 0;
  249. }
  250. static int kfd_parse_subtype(struct crat_subtype_generic *sub_type_hdr)
  251. {
  252. struct crat_subtype_computeunit *cu;
  253. struct crat_subtype_memory *mem;
  254. struct crat_subtype_cache *cache;
  255. struct crat_subtype_iolink *iolink;
  256. int ret = 0;
  257. switch (sub_type_hdr->type) {
  258. case CRAT_SUBTYPE_COMPUTEUNIT_AFFINITY:
  259. cu = (struct crat_subtype_computeunit *)sub_type_hdr;
  260. ret = kfd_parse_subtype_cu(cu);
  261. break;
  262. case CRAT_SUBTYPE_MEMORY_AFFINITY:
  263. mem = (struct crat_subtype_memory *)sub_type_hdr;
  264. ret = kfd_parse_subtype_mem(mem);
  265. break;
  266. case CRAT_SUBTYPE_CACHE_AFFINITY:
  267. cache = (struct crat_subtype_cache *)sub_type_hdr;
  268. ret = kfd_parse_subtype_cache(cache);
  269. break;
  270. case CRAT_SUBTYPE_TLB_AFFINITY:
  271. /*
  272. * For now, nothing to do here
  273. */
  274. pr_info("Found TLB entry in CRAT table (not processing)\n");
  275. break;
  276. case CRAT_SUBTYPE_CCOMPUTE_AFFINITY:
  277. /*
  278. * For now, nothing to do here
  279. */
  280. pr_info("Found CCOMPUTE entry in CRAT table (not processing)\n");
  281. break;
  282. case CRAT_SUBTYPE_IOLINK_AFFINITY:
  283. iolink = (struct crat_subtype_iolink *)sub_type_hdr;
  284. ret = kfd_parse_subtype_iolink(iolink);
  285. break;
  286. default:
  287. pr_warn("Unknown subtype (%d) in CRAT\n",
  288. sub_type_hdr->type);
  289. }
  290. return ret;
  291. }
  292. static void kfd_release_topology_device(struct kfd_topology_device *dev)
  293. {
  294. struct kfd_mem_properties *mem;
  295. struct kfd_cache_properties *cache;
  296. struct kfd_iolink_properties *iolink;
  297. list_del(&dev->list);
  298. while (dev->mem_props.next != &dev->mem_props) {
  299. mem = container_of(dev->mem_props.next,
  300. struct kfd_mem_properties, list);
  301. list_del(&mem->list);
  302. kfree(mem);
  303. }
  304. while (dev->cache_props.next != &dev->cache_props) {
  305. cache = container_of(dev->cache_props.next,
  306. struct kfd_cache_properties, list);
  307. list_del(&cache->list);
  308. kfree(cache);
  309. }
  310. while (dev->io_link_props.next != &dev->io_link_props) {
  311. iolink = container_of(dev->io_link_props.next,
  312. struct kfd_iolink_properties, list);
  313. list_del(&iolink->list);
  314. kfree(iolink);
  315. }
  316. kfree(dev);
  317. sys_props.num_devices--;
  318. }
  319. static void kfd_release_live_view(void)
  320. {
  321. struct kfd_topology_device *dev;
  322. while (topology_device_list.next != &topology_device_list) {
  323. dev = container_of(topology_device_list.next,
  324. struct kfd_topology_device, list);
  325. kfd_release_topology_device(dev);
  326. }
  327. memset(&sys_props, 0, sizeof(sys_props));
  328. }
  329. static struct kfd_topology_device *kfd_create_topology_device(void)
  330. {
  331. struct kfd_topology_device *dev;
  332. dev = kfd_alloc_struct(dev);
  333. if (!dev) {
  334. pr_err("No memory to allocate a topology device");
  335. return NULL;
  336. }
  337. INIT_LIST_HEAD(&dev->mem_props);
  338. INIT_LIST_HEAD(&dev->cache_props);
  339. INIT_LIST_HEAD(&dev->io_link_props);
  340. list_add_tail(&dev->list, &topology_device_list);
  341. sys_props.num_devices++;
  342. return dev;
  343. }
  344. static int kfd_parse_crat_table(void *crat_image)
  345. {
  346. struct kfd_topology_device *top_dev;
  347. struct crat_subtype_generic *sub_type_hdr;
  348. uint16_t node_id;
  349. int ret;
  350. struct crat_header *crat_table = (struct crat_header *)crat_image;
  351. uint16_t num_nodes;
  352. uint32_t image_len;
  353. if (!crat_image)
  354. return -EINVAL;
  355. num_nodes = crat_table->num_domains;
  356. image_len = crat_table->length;
  357. pr_info("Parsing CRAT table with %d nodes\n", num_nodes);
  358. for (node_id = 0; node_id < num_nodes; node_id++) {
  359. top_dev = kfd_create_topology_device();
  360. if (!top_dev) {
  361. kfd_release_live_view();
  362. return -ENOMEM;
  363. }
  364. }
  365. sys_props.platform_id =
  366. (*((uint64_t *)crat_table->oem_id)) & CRAT_OEMID_64BIT_MASK;
  367. sys_props.platform_oem = *((uint64_t *)crat_table->oem_table_id);
  368. sys_props.platform_rev = crat_table->revision;
  369. sub_type_hdr = (struct crat_subtype_generic *)(crat_table+1);
  370. while ((char *)sub_type_hdr + sizeof(struct crat_subtype_generic) <
  371. ((char *)crat_image) + image_len) {
  372. if (sub_type_hdr->flags & CRAT_SUBTYPE_FLAGS_ENABLED) {
  373. ret = kfd_parse_subtype(sub_type_hdr);
  374. if (ret != 0) {
  375. kfd_release_live_view();
  376. return ret;
  377. }
  378. }
  379. sub_type_hdr = (typeof(sub_type_hdr))((char *)sub_type_hdr +
  380. sub_type_hdr->length);
  381. }
  382. sys_props.generation_count++;
  383. topology_crat_parsed = 1;
  384. return 0;
  385. }
  386. #define sysfs_show_gen_prop(buffer, fmt, ...) \
  387. snprintf(buffer, PAGE_SIZE, "%s"fmt, buffer, __VA_ARGS__)
  388. #define sysfs_show_32bit_prop(buffer, name, value) \
  389. sysfs_show_gen_prop(buffer, "%s %u\n", name, value)
  390. #define sysfs_show_64bit_prop(buffer, name, value) \
  391. sysfs_show_gen_prop(buffer, "%s %llu\n", name, value)
  392. #define sysfs_show_32bit_val(buffer, value) \
  393. sysfs_show_gen_prop(buffer, "%u\n", value)
  394. #define sysfs_show_str_val(buffer, value) \
  395. sysfs_show_gen_prop(buffer, "%s\n", value)
  396. static ssize_t sysprops_show(struct kobject *kobj, struct attribute *attr,
  397. char *buffer)
  398. {
  399. ssize_t ret;
  400. /* Making sure that the buffer is an empty string */
  401. buffer[0] = 0;
  402. if (attr == &sys_props.attr_genid) {
  403. ret = sysfs_show_32bit_val(buffer, sys_props.generation_count);
  404. } else if (attr == &sys_props.attr_props) {
  405. sysfs_show_64bit_prop(buffer, "platform_oem",
  406. sys_props.platform_oem);
  407. sysfs_show_64bit_prop(buffer, "platform_id",
  408. sys_props.platform_id);
  409. ret = sysfs_show_64bit_prop(buffer, "platform_rev",
  410. sys_props.platform_rev);
  411. } else {
  412. ret = -EINVAL;
  413. }
  414. return ret;
  415. }
  416. static const struct sysfs_ops sysprops_ops = {
  417. .show = sysprops_show,
  418. };
  419. static struct kobj_type sysprops_type = {
  420. .sysfs_ops = &sysprops_ops,
  421. };
  422. static ssize_t iolink_show(struct kobject *kobj, struct attribute *attr,
  423. char *buffer)
  424. {
  425. ssize_t ret;
  426. struct kfd_iolink_properties *iolink;
  427. /* Making sure that the buffer is an empty string */
  428. buffer[0] = 0;
  429. iolink = container_of(attr, struct kfd_iolink_properties, attr);
  430. sysfs_show_32bit_prop(buffer, "type", iolink->iolink_type);
  431. sysfs_show_32bit_prop(buffer, "version_major", iolink->ver_maj);
  432. sysfs_show_32bit_prop(buffer, "version_minor", iolink->ver_min);
  433. sysfs_show_32bit_prop(buffer, "node_from", iolink->node_from);
  434. sysfs_show_32bit_prop(buffer, "node_to", iolink->node_to);
  435. sysfs_show_32bit_prop(buffer, "weight", iolink->weight);
  436. sysfs_show_32bit_prop(buffer, "min_latency", iolink->min_latency);
  437. sysfs_show_32bit_prop(buffer, "max_latency", iolink->max_latency);
  438. sysfs_show_32bit_prop(buffer, "min_bandwidth", iolink->min_bandwidth);
  439. sysfs_show_32bit_prop(buffer, "max_bandwidth", iolink->max_bandwidth);
  440. sysfs_show_32bit_prop(buffer, "recommended_transfer_size",
  441. iolink->rec_transfer_size);
  442. ret = sysfs_show_32bit_prop(buffer, "flags", iolink->flags);
  443. return ret;
  444. }
  445. static const struct sysfs_ops iolink_ops = {
  446. .show = iolink_show,
  447. };
  448. static struct kobj_type iolink_type = {
  449. .sysfs_ops = &iolink_ops,
  450. };
  451. static ssize_t mem_show(struct kobject *kobj, struct attribute *attr,
  452. char *buffer)
  453. {
  454. ssize_t ret;
  455. struct kfd_mem_properties *mem;
  456. /* Making sure that the buffer is an empty string */
  457. buffer[0] = 0;
  458. mem = container_of(attr, struct kfd_mem_properties, attr);
  459. sysfs_show_32bit_prop(buffer, "heap_type", mem->heap_type);
  460. sysfs_show_64bit_prop(buffer, "size_in_bytes", mem->size_in_bytes);
  461. sysfs_show_32bit_prop(buffer, "flags", mem->flags);
  462. sysfs_show_32bit_prop(buffer, "width", mem->width);
  463. ret = sysfs_show_32bit_prop(buffer, "mem_clk_max", mem->mem_clk_max);
  464. return ret;
  465. }
  466. static const struct sysfs_ops mem_ops = {
  467. .show = mem_show,
  468. };
  469. static struct kobj_type mem_type = {
  470. .sysfs_ops = &mem_ops,
  471. };
  472. static ssize_t kfd_cache_show(struct kobject *kobj, struct attribute *attr,
  473. char *buffer)
  474. {
  475. ssize_t ret;
  476. uint32_t i;
  477. struct kfd_cache_properties *cache;
  478. /* Making sure that the buffer is an empty string */
  479. buffer[0] = 0;
  480. cache = container_of(attr, struct kfd_cache_properties, attr);
  481. sysfs_show_32bit_prop(buffer, "processor_id_low",
  482. cache->processor_id_low);
  483. sysfs_show_32bit_prop(buffer, "level", cache->cache_level);
  484. sysfs_show_32bit_prop(buffer, "size", cache->cache_size);
  485. sysfs_show_32bit_prop(buffer, "cache_line_size", cache->cacheline_size);
  486. sysfs_show_32bit_prop(buffer, "cache_lines_per_tag",
  487. cache->cachelines_per_tag);
  488. sysfs_show_32bit_prop(buffer, "association", cache->cache_assoc);
  489. sysfs_show_32bit_prop(buffer, "latency", cache->cache_latency);
  490. sysfs_show_32bit_prop(buffer, "type", cache->cache_type);
  491. snprintf(buffer, PAGE_SIZE, "%ssibling_map ", buffer);
  492. for (i = 0; i < KFD_TOPOLOGY_CPU_SIBLINGS; i++)
  493. ret = snprintf(buffer, PAGE_SIZE, "%s%d%s",
  494. buffer, cache->sibling_map[i],
  495. (i == KFD_TOPOLOGY_CPU_SIBLINGS-1) ?
  496. "\n" : ",");
  497. return ret;
  498. }
  499. static const struct sysfs_ops cache_ops = {
  500. .show = kfd_cache_show,
  501. };
  502. static struct kobj_type cache_type = {
  503. .sysfs_ops = &cache_ops,
  504. };
  505. static ssize_t node_show(struct kobject *kobj, struct attribute *attr,
  506. char *buffer)
  507. {
  508. struct kfd_topology_device *dev;
  509. char public_name[KFD_TOPOLOGY_PUBLIC_NAME_SIZE];
  510. uint32_t i;
  511. uint32_t log_max_watch_addr;
  512. /* Making sure that the buffer is an empty string */
  513. buffer[0] = 0;
  514. if (strcmp(attr->name, "gpu_id") == 0) {
  515. dev = container_of(attr, struct kfd_topology_device,
  516. attr_gpuid);
  517. return sysfs_show_32bit_val(buffer, dev->gpu_id);
  518. }
  519. if (strcmp(attr->name, "name") == 0) {
  520. dev = container_of(attr, struct kfd_topology_device,
  521. attr_name);
  522. for (i = 0; i < KFD_TOPOLOGY_PUBLIC_NAME_SIZE; i++) {
  523. public_name[i] =
  524. (char)dev->node_props.marketing_name[i];
  525. if (dev->node_props.marketing_name[i] == 0)
  526. break;
  527. }
  528. public_name[KFD_TOPOLOGY_PUBLIC_NAME_SIZE-1] = 0x0;
  529. return sysfs_show_str_val(buffer, public_name);
  530. }
  531. dev = container_of(attr, struct kfd_topology_device,
  532. attr_props);
  533. sysfs_show_32bit_prop(buffer, "cpu_cores_count",
  534. dev->node_props.cpu_cores_count);
  535. sysfs_show_32bit_prop(buffer, "simd_count",
  536. dev->node_props.simd_count);
  537. if (dev->mem_bank_count < dev->node_props.mem_banks_count) {
  538. pr_info_once("mem_banks_count truncated from %d to %d\n",
  539. dev->node_props.mem_banks_count,
  540. dev->mem_bank_count);
  541. sysfs_show_32bit_prop(buffer, "mem_banks_count",
  542. dev->mem_bank_count);
  543. } else {
  544. sysfs_show_32bit_prop(buffer, "mem_banks_count",
  545. dev->node_props.mem_banks_count);
  546. }
  547. sysfs_show_32bit_prop(buffer, "caches_count",
  548. dev->node_props.caches_count);
  549. sysfs_show_32bit_prop(buffer, "io_links_count",
  550. dev->node_props.io_links_count);
  551. sysfs_show_32bit_prop(buffer, "cpu_core_id_base",
  552. dev->node_props.cpu_core_id_base);
  553. sysfs_show_32bit_prop(buffer, "simd_id_base",
  554. dev->node_props.simd_id_base);
  555. sysfs_show_32bit_prop(buffer, "max_waves_per_simd",
  556. dev->node_props.max_waves_per_simd);
  557. sysfs_show_32bit_prop(buffer, "lds_size_in_kb",
  558. dev->node_props.lds_size_in_kb);
  559. sysfs_show_32bit_prop(buffer, "gds_size_in_kb",
  560. dev->node_props.gds_size_in_kb);
  561. sysfs_show_32bit_prop(buffer, "wave_front_size",
  562. dev->node_props.wave_front_size);
  563. sysfs_show_32bit_prop(buffer, "array_count",
  564. dev->node_props.array_count);
  565. sysfs_show_32bit_prop(buffer, "simd_arrays_per_engine",
  566. dev->node_props.simd_arrays_per_engine);
  567. sysfs_show_32bit_prop(buffer, "cu_per_simd_array",
  568. dev->node_props.cu_per_simd_array);
  569. sysfs_show_32bit_prop(buffer, "simd_per_cu",
  570. dev->node_props.simd_per_cu);
  571. sysfs_show_32bit_prop(buffer, "max_slots_scratch_cu",
  572. dev->node_props.max_slots_scratch_cu);
  573. sysfs_show_32bit_prop(buffer, "vendor_id",
  574. dev->node_props.vendor_id);
  575. sysfs_show_32bit_prop(buffer, "device_id",
  576. dev->node_props.device_id);
  577. sysfs_show_32bit_prop(buffer, "location_id",
  578. dev->node_props.location_id);
  579. if (dev->gpu) {
  580. log_max_watch_addr =
  581. __ilog2_u32(dev->gpu->device_info->num_of_watch_points);
  582. if (log_max_watch_addr) {
  583. dev->node_props.capability |=
  584. HSA_CAP_WATCH_POINTS_SUPPORTED;
  585. dev->node_props.capability |=
  586. ((log_max_watch_addr <<
  587. HSA_CAP_WATCH_POINTS_TOTALBITS_SHIFT) &
  588. HSA_CAP_WATCH_POINTS_TOTALBITS_MASK);
  589. }
  590. sysfs_show_32bit_prop(buffer, "max_engine_clk_fcompute",
  591. dev->gpu->kfd2kgd->get_max_engine_clock_in_mhz(
  592. dev->gpu->kgd));
  593. sysfs_show_64bit_prop(buffer, "local_mem_size",
  594. (unsigned long long int) 0);
  595. sysfs_show_32bit_prop(buffer, "fw_version",
  596. dev->gpu->kfd2kgd->get_fw_version(
  597. dev->gpu->kgd,
  598. KGD_ENGINE_MEC1));
  599. sysfs_show_32bit_prop(buffer, "capability",
  600. dev->node_props.capability);
  601. }
  602. return sysfs_show_32bit_prop(buffer, "max_engine_clk_ccompute",
  603. cpufreq_quick_get_max(0)/1000);
  604. }
  605. static const struct sysfs_ops node_ops = {
  606. .show = node_show,
  607. };
  608. static struct kobj_type node_type = {
  609. .sysfs_ops = &node_ops,
  610. };
  611. static void kfd_remove_sysfs_file(struct kobject *kobj, struct attribute *attr)
  612. {
  613. sysfs_remove_file(kobj, attr);
  614. kobject_del(kobj);
  615. kobject_put(kobj);
  616. }
  617. static void kfd_remove_sysfs_node_entry(struct kfd_topology_device *dev)
  618. {
  619. struct kfd_iolink_properties *iolink;
  620. struct kfd_cache_properties *cache;
  621. struct kfd_mem_properties *mem;
  622. if (dev->kobj_iolink) {
  623. list_for_each_entry(iolink, &dev->io_link_props, list)
  624. if (iolink->kobj) {
  625. kfd_remove_sysfs_file(iolink->kobj,
  626. &iolink->attr);
  627. iolink->kobj = NULL;
  628. }
  629. kobject_del(dev->kobj_iolink);
  630. kobject_put(dev->kobj_iolink);
  631. dev->kobj_iolink = NULL;
  632. }
  633. if (dev->kobj_cache) {
  634. list_for_each_entry(cache, &dev->cache_props, list)
  635. if (cache->kobj) {
  636. kfd_remove_sysfs_file(cache->kobj,
  637. &cache->attr);
  638. cache->kobj = NULL;
  639. }
  640. kobject_del(dev->kobj_cache);
  641. kobject_put(dev->kobj_cache);
  642. dev->kobj_cache = NULL;
  643. }
  644. if (dev->kobj_mem) {
  645. list_for_each_entry(mem, &dev->mem_props, list)
  646. if (mem->kobj) {
  647. kfd_remove_sysfs_file(mem->kobj, &mem->attr);
  648. mem->kobj = NULL;
  649. }
  650. kobject_del(dev->kobj_mem);
  651. kobject_put(dev->kobj_mem);
  652. dev->kobj_mem = NULL;
  653. }
  654. if (dev->kobj_node) {
  655. sysfs_remove_file(dev->kobj_node, &dev->attr_gpuid);
  656. sysfs_remove_file(dev->kobj_node, &dev->attr_name);
  657. sysfs_remove_file(dev->kobj_node, &dev->attr_props);
  658. kobject_del(dev->kobj_node);
  659. kobject_put(dev->kobj_node);
  660. dev->kobj_node = NULL;
  661. }
  662. }
  663. static int kfd_build_sysfs_node_entry(struct kfd_topology_device *dev,
  664. uint32_t id)
  665. {
  666. struct kfd_iolink_properties *iolink;
  667. struct kfd_cache_properties *cache;
  668. struct kfd_mem_properties *mem;
  669. int ret;
  670. uint32_t i;
  671. if (WARN_ON(dev->kobj_node))
  672. return -EEXIST;
  673. /*
  674. * Creating the sysfs folders
  675. */
  676. dev->kobj_node = kfd_alloc_struct(dev->kobj_node);
  677. if (!dev->kobj_node)
  678. return -ENOMEM;
  679. ret = kobject_init_and_add(dev->kobj_node, &node_type,
  680. sys_props.kobj_nodes, "%d", id);
  681. if (ret < 0)
  682. return ret;
  683. dev->kobj_mem = kobject_create_and_add("mem_banks", dev->kobj_node);
  684. if (!dev->kobj_mem)
  685. return -ENOMEM;
  686. dev->kobj_cache = kobject_create_and_add("caches", dev->kobj_node);
  687. if (!dev->kobj_cache)
  688. return -ENOMEM;
  689. dev->kobj_iolink = kobject_create_and_add("io_links", dev->kobj_node);
  690. if (!dev->kobj_iolink)
  691. return -ENOMEM;
  692. /*
  693. * Creating sysfs files for node properties
  694. */
  695. dev->attr_gpuid.name = "gpu_id";
  696. dev->attr_gpuid.mode = KFD_SYSFS_FILE_MODE;
  697. sysfs_attr_init(&dev->attr_gpuid);
  698. dev->attr_name.name = "name";
  699. dev->attr_name.mode = KFD_SYSFS_FILE_MODE;
  700. sysfs_attr_init(&dev->attr_name);
  701. dev->attr_props.name = "properties";
  702. dev->attr_props.mode = KFD_SYSFS_FILE_MODE;
  703. sysfs_attr_init(&dev->attr_props);
  704. ret = sysfs_create_file(dev->kobj_node, &dev->attr_gpuid);
  705. if (ret < 0)
  706. return ret;
  707. ret = sysfs_create_file(dev->kobj_node, &dev->attr_name);
  708. if (ret < 0)
  709. return ret;
  710. ret = sysfs_create_file(dev->kobj_node, &dev->attr_props);
  711. if (ret < 0)
  712. return ret;
  713. i = 0;
  714. list_for_each_entry(mem, &dev->mem_props, list) {
  715. mem->kobj = kzalloc(sizeof(struct kobject), GFP_KERNEL);
  716. if (!mem->kobj)
  717. return -ENOMEM;
  718. ret = kobject_init_and_add(mem->kobj, &mem_type,
  719. dev->kobj_mem, "%d", i);
  720. if (ret < 0)
  721. return ret;
  722. mem->attr.name = "properties";
  723. mem->attr.mode = KFD_SYSFS_FILE_MODE;
  724. sysfs_attr_init(&mem->attr);
  725. ret = sysfs_create_file(mem->kobj, &mem->attr);
  726. if (ret < 0)
  727. return ret;
  728. i++;
  729. }
  730. i = 0;
  731. list_for_each_entry(cache, &dev->cache_props, list) {
  732. cache->kobj = kzalloc(sizeof(struct kobject), GFP_KERNEL);
  733. if (!cache->kobj)
  734. return -ENOMEM;
  735. ret = kobject_init_and_add(cache->kobj, &cache_type,
  736. dev->kobj_cache, "%d", i);
  737. if (ret < 0)
  738. return ret;
  739. cache->attr.name = "properties";
  740. cache->attr.mode = KFD_SYSFS_FILE_MODE;
  741. sysfs_attr_init(&cache->attr);
  742. ret = sysfs_create_file(cache->kobj, &cache->attr);
  743. if (ret < 0)
  744. return ret;
  745. i++;
  746. }
  747. i = 0;
  748. list_for_each_entry(iolink, &dev->io_link_props, list) {
  749. iolink->kobj = kzalloc(sizeof(struct kobject), GFP_KERNEL);
  750. if (!iolink->kobj)
  751. return -ENOMEM;
  752. ret = kobject_init_and_add(iolink->kobj, &iolink_type,
  753. dev->kobj_iolink, "%d", i);
  754. if (ret < 0)
  755. return ret;
  756. iolink->attr.name = "properties";
  757. iolink->attr.mode = KFD_SYSFS_FILE_MODE;
  758. sysfs_attr_init(&iolink->attr);
  759. ret = sysfs_create_file(iolink->kobj, &iolink->attr);
  760. if (ret < 0)
  761. return ret;
  762. i++;
  763. }
  764. return 0;
  765. }
  766. static int kfd_build_sysfs_node_tree(void)
  767. {
  768. struct kfd_topology_device *dev;
  769. int ret;
  770. uint32_t i = 0;
  771. list_for_each_entry(dev, &topology_device_list, list) {
  772. ret = kfd_build_sysfs_node_entry(dev, i);
  773. if (ret < 0)
  774. return ret;
  775. i++;
  776. }
  777. return 0;
  778. }
  779. static void kfd_remove_sysfs_node_tree(void)
  780. {
  781. struct kfd_topology_device *dev;
  782. list_for_each_entry(dev, &topology_device_list, list)
  783. kfd_remove_sysfs_node_entry(dev);
  784. }
  785. static int kfd_topology_update_sysfs(void)
  786. {
  787. int ret;
  788. pr_info("Creating topology SYSFS entries\n");
  789. if (!sys_props.kobj_topology) {
  790. sys_props.kobj_topology =
  791. kfd_alloc_struct(sys_props.kobj_topology);
  792. if (!sys_props.kobj_topology)
  793. return -ENOMEM;
  794. ret = kobject_init_and_add(sys_props.kobj_topology,
  795. &sysprops_type, &kfd_device->kobj,
  796. "topology");
  797. if (ret < 0)
  798. return ret;
  799. sys_props.kobj_nodes = kobject_create_and_add("nodes",
  800. sys_props.kobj_topology);
  801. if (!sys_props.kobj_nodes)
  802. return -ENOMEM;
  803. sys_props.attr_genid.name = "generation_id";
  804. sys_props.attr_genid.mode = KFD_SYSFS_FILE_MODE;
  805. sysfs_attr_init(&sys_props.attr_genid);
  806. ret = sysfs_create_file(sys_props.kobj_topology,
  807. &sys_props.attr_genid);
  808. if (ret < 0)
  809. return ret;
  810. sys_props.attr_props.name = "system_properties";
  811. sys_props.attr_props.mode = KFD_SYSFS_FILE_MODE;
  812. sysfs_attr_init(&sys_props.attr_props);
  813. ret = sysfs_create_file(sys_props.kobj_topology,
  814. &sys_props.attr_props);
  815. if (ret < 0)
  816. return ret;
  817. }
  818. kfd_remove_sysfs_node_tree();
  819. return kfd_build_sysfs_node_tree();
  820. }
  821. static void kfd_topology_release_sysfs(void)
  822. {
  823. kfd_remove_sysfs_node_tree();
  824. if (sys_props.kobj_topology) {
  825. sysfs_remove_file(sys_props.kobj_topology,
  826. &sys_props.attr_genid);
  827. sysfs_remove_file(sys_props.kobj_topology,
  828. &sys_props.attr_props);
  829. if (sys_props.kobj_nodes) {
  830. kobject_del(sys_props.kobj_nodes);
  831. kobject_put(sys_props.kobj_nodes);
  832. sys_props.kobj_nodes = NULL;
  833. }
  834. kobject_del(sys_props.kobj_topology);
  835. kobject_put(sys_props.kobj_topology);
  836. sys_props.kobj_topology = NULL;
  837. }
  838. }
  839. int kfd_topology_init(void)
  840. {
  841. void *crat_image = NULL;
  842. size_t image_size = 0;
  843. int ret;
  844. /*
  845. * Initialize the head for the topology device list
  846. */
  847. INIT_LIST_HEAD(&topology_device_list);
  848. init_rwsem(&topology_lock);
  849. topology_crat_parsed = 0;
  850. memset(&sys_props, 0, sizeof(sys_props));
  851. /*
  852. * Get the CRAT image from the ACPI
  853. */
  854. ret = kfd_topology_get_crat_acpi(crat_image, &image_size);
  855. if (ret == 0 && image_size > 0) {
  856. pr_info("Found CRAT image with size=%zd\n", image_size);
  857. crat_image = kmalloc(image_size, GFP_KERNEL);
  858. if (!crat_image) {
  859. ret = -ENOMEM;
  860. pr_err("No memory for allocating CRAT image\n");
  861. goto err;
  862. }
  863. ret = kfd_topology_get_crat_acpi(crat_image, &image_size);
  864. if (ret == 0) {
  865. down_write(&topology_lock);
  866. ret = kfd_parse_crat_table(crat_image);
  867. if (ret == 0)
  868. ret = kfd_topology_update_sysfs();
  869. up_write(&topology_lock);
  870. } else {
  871. pr_err("Couldn't get CRAT table size from ACPI\n");
  872. }
  873. kfree(crat_image);
  874. } else if (ret == -ENODATA) {
  875. ret = 0;
  876. } else {
  877. pr_err("Couldn't get CRAT table size from ACPI\n");
  878. }
  879. err:
  880. pr_info("Finished initializing topology ret=%d\n", ret);
  881. return ret;
  882. }
  883. void kfd_topology_shutdown(void)
  884. {
  885. kfd_topology_release_sysfs();
  886. kfd_release_live_view();
  887. }
  888. static void kfd_debug_print_topology(void)
  889. {
  890. struct kfd_topology_device *dev;
  891. uint32_t i = 0;
  892. pr_info("DEBUG PRINT OF TOPOLOGY:");
  893. list_for_each_entry(dev, &topology_device_list, list) {
  894. pr_info("Node: %d\n", i);
  895. pr_info("\tGPU assigned: %s\n", (dev->gpu ? "yes" : "no"));
  896. pr_info("\tCPU count: %d\n", dev->node_props.cpu_cores_count);
  897. pr_info("\tSIMD count: %d", dev->node_props.simd_count);
  898. i++;
  899. }
  900. }
  901. static uint32_t kfd_generate_gpu_id(struct kfd_dev *gpu)
  902. {
  903. uint32_t hashout;
  904. uint32_t buf[7];
  905. uint64_t local_mem_size;
  906. int i;
  907. if (!gpu)
  908. return 0;
  909. local_mem_size = gpu->kfd2kgd->get_vmem_size(gpu->kgd);
  910. buf[0] = gpu->pdev->devfn;
  911. buf[1] = gpu->pdev->subsystem_vendor;
  912. buf[2] = gpu->pdev->subsystem_device;
  913. buf[3] = gpu->pdev->device;
  914. buf[4] = gpu->pdev->bus->number;
  915. buf[5] = lower_32_bits(local_mem_size);
  916. buf[6] = upper_32_bits(local_mem_size);
  917. for (i = 0, hashout = 0; i < 7; i++)
  918. hashout ^= hash_32(buf[i], KFD_GPU_ID_HASH_WIDTH);
  919. return hashout;
  920. }
  921. static struct kfd_topology_device *kfd_assign_gpu(struct kfd_dev *gpu)
  922. {
  923. struct kfd_topology_device *dev;
  924. struct kfd_topology_device *out_dev = NULL;
  925. list_for_each_entry(dev, &topology_device_list, list)
  926. if (!dev->gpu && (dev->node_props.simd_count > 0)) {
  927. dev->gpu = gpu;
  928. out_dev = dev;
  929. break;
  930. }
  931. return out_dev;
  932. }
  933. static void kfd_notify_gpu_change(uint32_t gpu_id, int arrival)
  934. {
  935. /*
  936. * TODO: Generate an event for thunk about the arrival/removal
  937. * of the GPU
  938. */
  939. }
  940. int kfd_topology_add_device(struct kfd_dev *gpu)
  941. {
  942. uint32_t gpu_id;
  943. struct kfd_topology_device *dev;
  944. int res;
  945. gpu_id = kfd_generate_gpu_id(gpu);
  946. pr_debug("Adding new GPU (ID: 0x%x) to topology\n", gpu_id);
  947. down_write(&topology_lock);
  948. /*
  949. * Try to assign the GPU to existing topology device (generated from
  950. * CRAT table
  951. */
  952. dev = kfd_assign_gpu(gpu);
  953. if (!dev) {
  954. pr_info("GPU was not found in the current topology. Extending.\n");
  955. kfd_debug_print_topology();
  956. dev = kfd_create_topology_device();
  957. if (!dev) {
  958. res = -ENOMEM;
  959. goto err;
  960. }
  961. dev->gpu = gpu;
  962. /*
  963. * TODO: Make a call to retrieve topology information from the
  964. * GPU vBIOS
  965. */
  966. /* Update the SYSFS tree, since we added another topology
  967. * device
  968. */
  969. if (kfd_topology_update_sysfs() < 0)
  970. kfd_topology_release_sysfs();
  971. }
  972. dev->gpu_id = gpu_id;
  973. gpu->id = gpu_id;
  974. dev->node_props.vendor_id = gpu->pdev->vendor;
  975. dev->node_props.device_id = gpu->pdev->device;
  976. dev->node_props.location_id = (gpu->pdev->bus->number << 24) +
  977. (gpu->pdev->devfn & 0xffffff);
  978. /*
  979. * TODO: Retrieve max engine clock values from KGD
  980. */
  981. if (dev->gpu->device_info->asic_family == CHIP_CARRIZO) {
  982. dev->node_props.capability |= HSA_CAP_DOORBELL_PACKET_TYPE;
  983. pr_info("Adding doorbell packet type capability\n");
  984. }
  985. res = 0;
  986. err:
  987. up_write(&topology_lock);
  988. if (res == 0)
  989. kfd_notify_gpu_change(gpu_id, 1);
  990. return res;
  991. }
  992. int kfd_topology_remove_device(struct kfd_dev *gpu)
  993. {
  994. struct kfd_topology_device *dev;
  995. uint32_t gpu_id;
  996. int res = -ENODEV;
  997. down_write(&topology_lock);
  998. list_for_each_entry(dev, &topology_device_list, list)
  999. if (dev->gpu == gpu) {
  1000. gpu_id = dev->gpu_id;
  1001. kfd_remove_sysfs_node_entry(dev);
  1002. kfd_release_topology_device(dev);
  1003. res = 0;
  1004. if (kfd_topology_update_sysfs() < 0)
  1005. kfd_topology_release_sysfs();
  1006. break;
  1007. }
  1008. up_write(&topology_lock);
  1009. if (res == 0)
  1010. kfd_notify_gpu_change(gpu_id, 0);
  1011. return res;
  1012. }
  1013. /*
  1014. * When idx is out of bounds, the function will return NULL
  1015. */
  1016. struct kfd_dev *kfd_topology_enum_kfd_devices(uint8_t idx)
  1017. {
  1018. struct kfd_topology_device *top_dev;
  1019. struct kfd_dev *device = NULL;
  1020. uint8_t device_idx = 0;
  1021. down_read(&topology_lock);
  1022. list_for_each_entry(top_dev, &topology_device_list, list) {
  1023. if (device_idx == idx) {
  1024. device = top_dev->gpu;
  1025. break;
  1026. }
  1027. device_idx++;
  1028. }
  1029. up_read(&topology_lock);
  1030. return device;
  1031. }