kfd_topology.c 32 KB

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