kfd_topology.c 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414
  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 <linux/dmi.h>
  31. #include <linux/atomic.h>
  32. #include "kfd_priv.h"
  33. #include "kfd_crat.h"
  34. #include "kfd_topology.h"
  35. #include "kfd_device_queue_manager.h"
  36. /* topology_device_list - Master list of all topology devices */
  37. static struct list_head topology_device_list;
  38. static struct kfd_system_properties sys_props;
  39. static DECLARE_RWSEM(topology_lock);
  40. static atomic_t topology_crat_proximity_domain;
  41. struct kfd_topology_device *kfd_topology_device_by_proximity_domain(
  42. uint32_t proximity_domain)
  43. {
  44. struct kfd_topology_device *top_dev;
  45. struct kfd_topology_device *device = NULL;
  46. down_read(&topology_lock);
  47. list_for_each_entry(top_dev, &topology_device_list, list)
  48. if (top_dev->proximity_domain == proximity_domain) {
  49. device = top_dev;
  50. break;
  51. }
  52. up_read(&topology_lock);
  53. return device;
  54. }
  55. struct kfd_dev *kfd_device_by_id(uint32_t gpu_id)
  56. {
  57. struct kfd_topology_device *top_dev;
  58. struct kfd_dev *device = NULL;
  59. down_read(&topology_lock);
  60. list_for_each_entry(top_dev, &topology_device_list, list)
  61. if (top_dev->gpu_id == gpu_id) {
  62. device = top_dev->gpu;
  63. break;
  64. }
  65. up_read(&topology_lock);
  66. return device;
  67. }
  68. struct kfd_dev *kfd_device_by_pci_dev(const struct pci_dev *pdev)
  69. {
  70. struct kfd_topology_device *top_dev;
  71. struct kfd_dev *device = NULL;
  72. down_read(&topology_lock);
  73. list_for_each_entry(top_dev, &topology_device_list, list)
  74. if (top_dev->gpu->pdev == pdev) {
  75. device = top_dev->gpu;
  76. break;
  77. }
  78. up_read(&topology_lock);
  79. return device;
  80. }
  81. /* Called with write topology_lock acquired */
  82. static void kfd_release_topology_device(struct kfd_topology_device *dev)
  83. {
  84. struct kfd_mem_properties *mem;
  85. struct kfd_cache_properties *cache;
  86. struct kfd_iolink_properties *iolink;
  87. struct kfd_perf_properties *perf;
  88. list_del(&dev->list);
  89. while (dev->mem_props.next != &dev->mem_props) {
  90. mem = container_of(dev->mem_props.next,
  91. struct kfd_mem_properties, list);
  92. list_del(&mem->list);
  93. kfree(mem);
  94. }
  95. while (dev->cache_props.next != &dev->cache_props) {
  96. cache = container_of(dev->cache_props.next,
  97. struct kfd_cache_properties, list);
  98. list_del(&cache->list);
  99. kfree(cache);
  100. }
  101. while (dev->io_link_props.next != &dev->io_link_props) {
  102. iolink = container_of(dev->io_link_props.next,
  103. struct kfd_iolink_properties, list);
  104. list_del(&iolink->list);
  105. kfree(iolink);
  106. }
  107. while (dev->perf_props.next != &dev->perf_props) {
  108. perf = container_of(dev->perf_props.next,
  109. struct kfd_perf_properties, list);
  110. list_del(&perf->list);
  111. kfree(perf);
  112. }
  113. kfree(dev);
  114. }
  115. void kfd_release_topology_device_list(struct list_head *device_list)
  116. {
  117. struct kfd_topology_device *dev;
  118. while (!list_empty(device_list)) {
  119. dev = list_first_entry(device_list,
  120. struct kfd_topology_device, list);
  121. kfd_release_topology_device(dev);
  122. }
  123. }
  124. static void kfd_release_live_view(void)
  125. {
  126. kfd_release_topology_device_list(&topology_device_list);
  127. memset(&sys_props, 0, sizeof(sys_props));
  128. }
  129. struct kfd_topology_device *kfd_create_topology_device(
  130. struct list_head *device_list)
  131. {
  132. struct kfd_topology_device *dev;
  133. dev = kfd_alloc_struct(dev);
  134. if (!dev) {
  135. pr_err("No memory to allocate a topology device");
  136. return NULL;
  137. }
  138. INIT_LIST_HEAD(&dev->mem_props);
  139. INIT_LIST_HEAD(&dev->cache_props);
  140. INIT_LIST_HEAD(&dev->io_link_props);
  141. INIT_LIST_HEAD(&dev->perf_props);
  142. list_add_tail(&dev->list, device_list);
  143. return dev;
  144. }
  145. #define sysfs_show_gen_prop(buffer, fmt, ...) \
  146. snprintf(buffer, PAGE_SIZE, "%s"fmt, buffer, __VA_ARGS__)
  147. #define sysfs_show_32bit_prop(buffer, name, value) \
  148. sysfs_show_gen_prop(buffer, "%s %u\n", name, value)
  149. #define sysfs_show_64bit_prop(buffer, name, value) \
  150. sysfs_show_gen_prop(buffer, "%s %llu\n", name, value)
  151. #define sysfs_show_32bit_val(buffer, value) \
  152. sysfs_show_gen_prop(buffer, "%u\n", value)
  153. #define sysfs_show_str_val(buffer, value) \
  154. sysfs_show_gen_prop(buffer, "%s\n", value)
  155. static ssize_t sysprops_show(struct kobject *kobj, struct attribute *attr,
  156. char *buffer)
  157. {
  158. ssize_t ret;
  159. /* Making sure that the buffer is an empty string */
  160. buffer[0] = 0;
  161. if (attr == &sys_props.attr_genid) {
  162. ret = sysfs_show_32bit_val(buffer, sys_props.generation_count);
  163. } else if (attr == &sys_props.attr_props) {
  164. sysfs_show_64bit_prop(buffer, "platform_oem",
  165. sys_props.platform_oem);
  166. sysfs_show_64bit_prop(buffer, "platform_id",
  167. sys_props.platform_id);
  168. ret = sysfs_show_64bit_prop(buffer, "platform_rev",
  169. sys_props.platform_rev);
  170. } else {
  171. ret = -EINVAL;
  172. }
  173. return ret;
  174. }
  175. static void kfd_topology_kobj_release(struct kobject *kobj)
  176. {
  177. kfree(kobj);
  178. }
  179. static const struct sysfs_ops sysprops_ops = {
  180. .show = sysprops_show,
  181. };
  182. static struct kobj_type sysprops_type = {
  183. .release = kfd_topology_kobj_release,
  184. .sysfs_ops = &sysprops_ops,
  185. };
  186. static ssize_t iolink_show(struct kobject *kobj, struct attribute *attr,
  187. char *buffer)
  188. {
  189. ssize_t ret;
  190. struct kfd_iolink_properties *iolink;
  191. /* Making sure that the buffer is an empty string */
  192. buffer[0] = 0;
  193. iolink = container_of(attr, struct kfd_iolink_properties, attr);
  194. sysfs_show_32bit_prop(buffer, "type", iolink->iolink_type);
  195. sysfs_show_32bit_prop(buffer, "version_major", iolink->ver_maj);
  196. sysfs_show_32bit_prop(buffer, "version_minor", iolink->ver_min);
  197. sysfs_show_32bit_prop(buffer, "node_from", iolink->node_from);
  198. sysfs_show_32bit_prop(buffer, "node_to", iolink->node_to);
  199. sysfs_show_32bit_prop(buffer, "weight", iolink->weight);
  200. sysfs_show_32bit_prop(buffer, "min_latency", iolink->min_latency);
  201. sysfs_show_32bit_prop(buffer, "max_latency", iolink->max_latency);
  202. sysfs_show_32bit_prop(buffer, "min_bandwidth", iolink->min_bandwidth);
  203. sysfs_show_32bit_prop(buffer, "max_bandwidth", iolink->max_bandwidth);
  204. sysfs_show_32bit_prop(buffer, "recommended_transfer_size",
  205. iolink->rec_transfer_size);
  206. ret = sysfs_show_32bit_prop(buffer, "flags", iolink->flags);
  207. return ret;
  208. }
  209. static const struct sysfs_ops iolink_ops = {
  210. .show = iolink_show,
  211. };
  212. static struct kobj_type iolink_type = {
  213. .release = kfd_topology_kobj_release,
  214. .sysfs_ops = &iolink_ops,
  215. };
  216. static ssize_t mem_show(struct kobject *kobj, struct attribute *attr,
  217. char *buffer)
  218. {
  219. ssize_t ret;
  220. struct kfd_mem_properties *mem;
  221. /* Making sure that the buffer is an empty string */
  222. buffer[0] = 0;
  223. mem = container_of(attr, struct kfd_mem_properties, attr);
  224. sysfs_show_32bit_prop(buffer, "heap_type", mem->heap_type);
  225. sysfs_show_64bit_prop(buffer, "size_in_bytes", mem->size_in_bytes);
  226. sysfs_show_32bit_prop(buffer, "flags", mem->flags);
  227. sysfs_show_32bit_prop(buffer, "width", mem->width);
  228. ret = sysfs_show_32bit_prop(buffer, "mem_clk_max", mem->mem_clk_max);
  229. return ret;
  230. }
  231. static const struct sysfs_ops mem_ops = {
  232. .show = mem_show,
  233. };
  234. static struct kobj_type mem_type = {
  235. .release = kfd_topology_kobj_release,
  236. .sysfs_ops = &mem_ops,
  237. };
  238. static ssize_t kfd_cache_show(struct kobject *kobj, struct attribute *attr,
  239. char *buffer)
  240. {
  241. ssize_t ret;
  242. uint32_t i, j;
  243. struct kfd_cache_properties *cache;
  244. /* Making sure that the buffer is an empty string */
  245. buffer[0] = 0;
  246. cache = container_of(attr, struct kfd_cache_properties, attr);
  247. sysfs_show_32bit_prop(buffer, "processor_id_low",
  248. cache->processor_id_low);
  249. sysfs_show_32bit_prop(buffer, "level", cache->cache_level);
  250. sysfs_show_32bit_prop(buffer, "size", cache->cache_size);
  251. sysfs_show_32bit_prop(buffer, "cache_line_size", cache->cacheline_size);
  252. sysfs_show_32bit_prop(buffer, "cache_lines_per_tag",
  253. cache->cachelines_per_tag);
  254. sysfs_show_32bit_prop(buffer, "association", cache->cache_assoc);
  255. sysfs_show_32bit_prop(buffer, "latency", cache->cache_latency);
  256. sysfs_show_32bit_prop(buffer, "type", cache->cache_type);
  257. snprintf(buffer, PAGE_SIZE, "%ssibling_map ", buffer);
  258. for (i = 0; i < CRAT_SIBLINGMAP_SIZE; i++)
  259. for (j = 0; j < sizeof(cache->sibling_map[0])*8; j++) {
  260. /* Check each bit */
  261. if (cache->sibling_map[i] & (1 << j))
  262. ret = snprintf(buffer, PAGE_SIZE,
  263. "%s%d%s", buffer, 1, ",");
  264. else
  265. ret = snprintf(buffer, PAGE_SIZE,
  266. "%s%d%s", buffer, 0, ",");
  267. }
  268. /* Replace the last "," with end of line */
  269. *(buffer + strlen(buffer) - 1) = 0xA;
  270. return ret;
  271. }
  272. static const struct sysfs_ops cache_ops = {
  273. .show = kfd_cache_show,
  274. };
  275. static struct kobj_type cache_type = {
  276. .release = kfd_topology_kobj_release,
  277. .sysfs_ops = &cache_ops,
  278. };
  279. /****** Sysfs of Performance Counters ******/
  280. struct kfd_perf_attr {
  281. struct kobj_attribute attr;
  282. uint32_t data;
  283. };
  284. static ssize_t perf_show(struct kobject *kobj, struct kobj_attribute *attrs,
  285. char *buf)
  286. {
  287. struct kfd_perf_attr *attr;
  288. buf[0] = 0;
  289. attr = container_of(attrs, struct kfd_perf_attr, attr);
  290. if (!attr->data) /* invalid data for PMC */
  291. return 0;
  292. else
  293. return sysfs_show_32bit_val(buf, attr->data);
  294. }
  295. #define KFD_PERF_DESC(_name, _data) \
  296. { \
  297. .attr = __ATTR(_name, 0444, perf_show, NULL), \
  298. .data = _data, \
  299. }
  300. static struct kfd_perf_attr perf_attr_iommu[] = {
  301. KFD_PERF_DESC(max_concurrent, 0),
  302. KFD_PERF_DESC(num_counters, 0),
  303. KFD_PERF_DESC(counter_ids, 0),
  304. };
  305. /****************************************/
  306. static ssize_t node_show(struct kobject *kobj, struct attribute *attr,
  307. char *buffer)
  308. {
  309. struct kfd_topology_device *dev;
  310. char public_name[KFD_TOPOLOGY_PUBLIC_NAME_SIZE];
  311. uint32_t i;
  312. uint32_t log_max_watch_addr;
  313. /* Making sure that the buffer is an empty string */
  314. buffer[0] = 0;
  315. if (strcmp(attr->name, "gpu_id") == 0) {
  316. dev = container_of(attr, struct kfd_topology_device,
  317. attr_gpuid);
  318. return sysfs_show_32bit_val(buffer, dev->gpu_id);
  319. }
  320. if (strcmp(attr->name, "name") == 0) {
  321. dev = container_of(attr, struct kfd_topology_device,
  322. attr_name);
  323. for (i = 0; i < KFD_TOPOLOGY_PUBLIC_NAME_SIZE; i++) {
  324. public_name[i] =
  325. (char)dev->node_props.marketing_name[i];
  326. if (dev->node_props.marketing_name[i] == 0)
  327. break;
  328. }
  329. public_name[KFD_TOPOLOGY_PUBLIC_NAME_SIZE-1] = 0x0;
  330. return sysfs_show_str_val(buffer, public_name);
  331. }
  332. dev = container_of(attr, struct kfd_topology_device,
  333. attr_props);
  334. sysfs_show_32bit_prop(buffer, "cpu_cores_count",
  335. dev->node_props.cpu_cores_count);
  336. sysfs_show_32bit_prop(buffer, "simd_count",
  337. dev->node_props.simd_count);
  338. sysfs_show_32bit_prop(buffer, "mem_banks_count",
  339. dev->node_props.mem_banks_count);
  340. sysfs_show_32bit_prop(buffer, "caches_count",
  341. dev->node_props.caches_count);
  342. sysfs_show_32bit_prop(buffer, "io_links_count",
  343. dev->node_props.io_links_count);
  344. sysfs_show_32bit_prop(buffer, "cpu_core_id_base",
  345. dev->node_props.cpu_core_id_base);
  346. sysfs_show_32bit_prop(buffer, "simd_id_base",
  347. dev->node_props.simd_id_base);
  348. sysfs_show_32bit_prop(buffer, "max_waves_per_simd",
  349. dev->node_props.max_waves_per_simd);
  350. sysfs_show_32bit_prop(buffer, "lds_size_in_kb",
  351. dev->node_props.lds_size_in_kb);
  352. sysfs_show_32bit_prop(buffer, "gds_size_in_kb",
  353. dev->node_props.gds_size_in_kb);
  354. sysfs_show_32bit_prop(buffer, "wave_front_size",
  355. dev->node_props.wave_front_size);
  356. sysfs_show_32bit_prop(buffer, "array_count",
  357. dev->node_props.array_count);
  358. sysfs_show_32bit_prop(buffer, "simd_arrays_per_engine",
  359. dev->node_props.simd_arrays_per_engine);
  360. sysfs_show_32bit_prop(buffer, "cu_per_simd_array",
  361. dev->node_props.cu_per_simd_array);
  362. sysfs_show_32bit_prop(buffer, "simd_per_cu",
  363. dev->node_props.simd_per_cu);
  364. sysfs_show_32bit_prop(buffer, "max_slots_scratch_cu",
  365. dev->node_props.max_slots_scratch_cu);
  366. sysfs_show_32bit_prop(buffer, "vendor_id",
  367. dev->node_props.vendor_id);
  368. sysfs_show_32bit_prop(buffer, "device_id",
  369. dev->node_props.device_id);
  370. sysfs_show_32bit_prop(buffer, "location_id",
  371. dev->node_props.location_id);
  372. if (dev->gpu) {
  373. log_max_watch_addr =
  374. __ilog2_u32(dev->gpu->device_info->num_of_watch_points);
  375. if (log_max_watch_addr) {
  376. dev->node_props.capability |=
  377. HSA_CAP_WATCH_POINTS_SUPPORTED;
  378. dev->node_props.capability |=
  379. ((log_max_watch_addr <<
  380. HSA_CAP_WATCH_POINTS_TOTALBITS_SHIFT) &
  381. HSA_CAP_WATCH_POINTS_TOTALBITS_MASK);
  382. }
  383. if (dev->gpu->device_info->asic_family == CHIP_TONGA)
  384. dev->node_props.capability |=
  385. HSA_CAP_AQL_QUEUE_DOUBLE_MAP;
  386. sysfs_show_32bit_prop(buffer, "max_engine_clk_fcompute",
  387. dev->node_props.max_engine_clk_fcompute);
  388. sysfs_show_64bit_prop(buffer, "local_mem_size",
  389. (unsigned long long int) 0);
  390. sysfs_show_32bit_prop(buffer, "fw_version",
  391. dev->gpu->kfd2kgd->get_fw_version(
  392. dev->gpu->kgd,
  393. KGD_ENGINE_MEC1));
  394. sysfs_show_32bit_prop(buffer, "capability",
  395. dev->node_props.capability);
  396. }
  397. return sysfs_show_32bit_prop(buffer, "max_engine_clk_ccompute",
  398. cpufreq_quick_get_max(0)/1000);
  399. }
  400. static const struct sysfs_ops node_ops = {
  401. .show = node_show,
  402. };
  403. static struct kobj_type node_type = {
  404. .release = kfd_topology_kobj_release,
  405. .sysfs_ops = &node_ops,
  406. };
  407. static void kfd_remove_sysfs_file(struct kobject *kobj, struct attribute *attr)
  408. {
  409. sysfs_remove_file(kobj, attr);
  410. kobject_del(kobj);
  411. kobject_put(kobj);
  412. }
  413. static void kfd_remove_sysfs_node_entry(struct kfd_topology_device *dev)
  414. {
  415. struct kfd_iolink_properties *iolink;
  416. struct kfd_cache_properties *cache;
  417. struct kfd_mem_properties *mem;
  418. struct kfd_perf_properties *perf;
  419. if (dev->kobj_iolink) {
  420. list_for_each_entry(iolink, &dev->io_link_props, list)
  421. if (iolink->kobj) {
  422. kfd_remove_sysfs_file(iolink->kobj,
  423. &iolink->attr);
  424. iolink->kobj = NULL;
  425. }
  426. kobject_del(dev->kobj_iolink);
  427. kobject_put(dev->kobj_iolink);
  428. dev->kobj_iolink = NULL;
  429. }
  430. if (dev->kobj_cache) {
  431. list_for_each_entry(cache, &dev->cache_props, list)
  432. if (cache->kobj) {
  433. kfd_remove_sysfs_file(cache->kobj,
  434. &cache->attr);
  435. cache->kobj = NULL;
  436. }
  437. kobject_del(dev->kobj_cache);
  438. kobject_put(dev->kobj_cache);
  439. dev->kobj_cache = NULL;
  440. }
  441. if (dev->kobj_mem) {
  442. list_for_each_entry(mem, &dev->mem_props, list)
  443. if (mem->kobj) {
  444. kfd_remove_sysfs_file(mem->kobj, &mem->attr);
  445. mem->kobj = NULL;
  446. }
  447. kobject_del(dev->kobj_mem);
  448. kobject_put(dev->kobj_mem);
  449. dev->kobj_mem = NULL;
  450. }
  451. if (dev->kobj_perf) {
  452. list_for_each_entry(perf, &dev->perf_props, list) {
  453. kfree(perf->attr_group);
  454. perf->attr_group = NULL;
  455. }
  456. kobject_del(dev->kobj_perf);
  457. kobject_put(dev->kobj_perf);
  458. dev->kobj_perf = NULL;
  459. }
  460. if (dev->kobj_node) {
  461. sysfs_remove_file(dev->kobj_node, &dev->attr_gpuid);
  462. sysfs_remove_file(dev->kobj_node, &dev->attr_name);
  463. sysfs_remove_file(dev->kobj_node, &dev->attr_props);
  464. kobject_del(dev->kobj_node);
  465. kobject_put(dev->kobj_node);
  466. dev->kobj_node = NULL;
  467. }
  468. }
  469. static int kfd_build_sysfs_node_entry(struct kfd_topology_device *dev,
  470. uint32_t id)
  471. {
  472. struct kfd_iolink_properties *iolink;
  473. struct kfd_cache_properties *cache;
  474. struct kfd_mem_properties *mem;
  475. struct kfd_perf_properties *perf;
  476. int ret;
  477. uint32_t i, num_attrs;
  478. struct attribute **attrs;
  479. if (WARN_ON(dev->kobj_node))
  480. return -EEXIST;
  481. /*
  482. * Creating the sysfs folders
  483. */
  484. dev->kobj_node = kfd_alloc_struct(dev->kobj_node);
  485. if (!dev->kobj_node)
  486. return -ENOMEM;
  487. ret = kobject_init_and_add(dev->kobj_node, &node_type,
  488. sys_props.kobj_nodes, "%d", id);
  489. if (ret < 0)
  490. return ret;
  491. dev->kobj_mem = kobject_create_and_add("mem_banks", dev->kobj_node);
  492. if (!dev->kobj_mem)
  493. return -ENOMEM;
  494. dev->kobj_cache = kobject_create_and_add("caches", dev->kobj_node);
  495. if (!dev->kobj_cache)
  496. return -ENOMEM;
  497. dev->kobj_iolink = kobject_create_and_add("io_links", dev->kobj_node);
  498. if (!dev->kobj_iolink)
  499. return -ENOMEM;
  500. dev->kobj_perf = kobject_create_and_add("perf", dev->kobj_node);
  501. if (!dev->kobj_perf)
  502. return -ENOMEM;
  503. /*
  504. * Creating sysfs files for node properties
  505. */
  506. dev->attr_gpuid.name = "gpu_id";
  507. dev->attr_gpuid.mode = KFD_SYSFS_FILE_MODE;
  508. sysfs_attr_init(&dev->attr_gpuid);
  509. dev->attr_name.name = "name";
  510. dev->attr_name.mode = KFD_SYSFS_FILE_MODE;
  511. sysfs_attr_init(&dev->attr_name);
  512. dev->attr_props.name = "properties";
  513. dev->attr_props.mode = KFD_SYSFS_FILE_MODE;
  514. sysfs_attr_init(&dev->attr_props);
  515. ret = sysfs_create_file(dev->kobj_node, &dev->attr_gpuid);
  516. if (ret < 0)
  517. return ret;
  518. ret = sysfs_create_file(dev->kobj_node, &dev->attr_name);
  519. if (ret < 0)
  520. return ret;
  521. ret = sysfs_create_file(dev->kobj_node, &dev->attr_props);
  522. if (ret < 0)
  523. return ret;
  524. i = 0;
  525. list_for_each_entry(mem, &dev->mem_props, list) {
  526. mem->kobj = kzalloc(sizeof(struct kobject), GFP_KERNEL);
  527. if (!mem->kobj)
  528. return -ENOMEM;
  529. ret = kobject_init_and_add(mem->kobj, &mem_type,
  530. dev->kobj_mem, "%d", i);
  531. if (ret < 0)
  532. return ret;
  533. mem->attr.name = "properties";
  534. mem->attr.mode = KFD_SYSFS_FILE_MODE;
  535. sysfs_attr_init(&mem->attr);
  536. ret = sysfs_create_file(mem->kobj, &mem->attr);
  537. if (ret < 0)
  538. return ret;
  539. i++;
  540. }
  541. i = 0;
  542. list_for_each_entry(cache, &dev->cache_props, list) {
  543. cache->kobj = kzalloc(sizeof(struct kobject), GFP_KERNEL);
  544. if (!cache->kobj)
  545. return -ENOMEM;
  546. ret = kobject_init_and_add(cache->kobj, &cache_type,
  547. dev->kobj_cache, "%d", i);
  548. if (ret < 0)
  549. return ret;
  550. cache->attr.name = "properties";
  551. cache->attr.mode = KFD_SYSFS_FILE_MODE;
  552. sysfs_attr_init(&cache->attr);
  553. ret = sysfs_create_file(cache->kobj, &cache->attr);
  554. if (ret < 0)
  555. return ret;
  556. i++;
  557. }
  558. i = 0;
  559. list_for_each_entry(iolink, &dev->io_link_props, list) {
  560. iolink->kobj = kzalloc(sizeof(struct kobject), GFP_KERNEL);
  561. if (!iolink->kobj)
  562. return -ENOMEM;
  563. ret = kobject_init_and_add(iolink->kobj, &iolink_type,
  564. dev->kobj_iolink, "%d", i);
  565. if (ret < 0)
  566. return ret;
  567. iolink->attr.name = "properties";
  568. iolink->attr.mode = KFD_SYSFS_FILE_MODE;
  569. sysfs_attr_init(&iolink->attr);
  570. ret = sysfs_create_file(iolink->kobj, &iolink->attr);
  571. if (ret < 0)
  572. return ret;
  573. i++;
  574. }
  575. /* All hardware blocks have the same number of attributes. */
  576. num_attrs = sizeof(perf_attr_iommu)/sizeof(struct kfd_perf_attr);
  577. list_for_each_entry(perf, &dev->perf_props, list) {
  578. perf->attr_group = kzalloc(sizeof(struct kfd_perf_attr)
  579. * num_attrs + sizeof(struct attribute_group),
  580. GFP_KERNEL);
  581. if (!perf->attr_group)
  582. return -ENOMEM;
  583. attrs = (struct attribute **)(perf->attr_group + 1);
  584. if (!strcmp(perf->block_name, "iommu")) {
  585. /* Information of IOMMU's num_counters and counter_ids is shown
  586. * under /sys/bus/event_source/devices/amd_iommu. We don't
  587. * duplicate here.
  588. */
  589. perf_attr_iommu[0].data = perf->max_concurrent;
  590. for (i = 0; i < num_attrs; i++)
  591. attrs[i] = &perf_attr_iommu[i].attr.attr;
  592. }
  593. perf->attr_group->name = perf->block_name;
  594. perf->attr_group->attrs = attrs;
  595. ret = sysfs_create_group(dev->kobj_perf, perf->attr_group);
  596. if (ret < 0)
  597. return ret;
  598. }
  599. return 0;
  600. }
  601. /* Called with write topology lock acquired */
  602. static int kfd_build_sysfs_node_tree(void)
  603. {
  604. struct kfd_topology_device *dev;
  605. int ret;
  606. uint32_t i = 0;
  607. list_for_each_entry(dev, &topology_device_list, list) {
  608. ret = kfd_build_sysfs_node_entry(dev, i);
  609. if (ret < 0)
  610. return ret;
  611. i++;
  612. }
  613. return 0;
  614. }
  615. /* Called with write topology lock acquired */
  616. static void kfd_remove_sysfs_node_tree(void)
  617. {
  618. struct kfd_topology_device *dev;
  619. list_for_each_entry(dev, &topology_device_list, list)
  620. kfd_remove_sysfs_node_entry(dev);
  621. }
  622. static int kfd_topology_update_sysfs(void)
  623. {
  624. int ret;
  625. pr_info("Creating topology SYSFS entries\n");
  626. if (!sys_props.kobj_topology) {
  627. sys_props.kobj_topology =
  628. kfd_alloc_struct(sys_props.kobj_topology);
  629. if (!sys_props.kobj_topology)
  630. return -ENOMEM;
  631. ret = kobject_init_and_add(sys_props.kobj_topology,
  632. &sysprops_type, &kfd_device->kobj,
  633. "topology");
  634. if (ret < 0)
  635. return ret;
  636. sys_props.kobj_nodes = kobject_create_and_add("nodes",
  637. sys_props.kobj_topology);
  638. if (!sys_props.kobj_nodes)
  639. return -ENOMEM;
  640. sys_props.attr_genid.name = "generation_id";
  641. sys_props.attr_genid.mode = KFD_SYSFS_FILE_MODE;
  642. sysfs_attr_init(&sys_props.attr_genid);
  643. ret = sysfs_create_file(sys_props.kobj_topology,
  644. &sys_props.attr_genid);
  645. if (ret < 0)
  646. return ret;
  647. sys_props.attr_props.name = "system_properties";
  648. sys_props.attr_props.mode = KFD_SYSFS_FILE_MODE;
  649. sysfs_attr_init(&sys_props.attr_props);
  650. ret = sysfs_create_file(sys_props.kobj_topology,
  651. &sys_props.attr_props);
  652. if (ret < 0)
  653. return ret;
  654. }
  655. kfd_remove_sysfs_node_tree();
  656. return kfd_build_sysfs_node_tree();
  657. }
  658. static void kfd_topology_release_sysfs(void)
  659. {
  660. kfd_remove_sysfs_node_tree();
  661. if (sys_props.kobj_topology) {
  662. sysfs_remove_file(sys_props.kobj_topology,
  663. &sys_props.attr_genid);
  664. sysfs_remove_file(sys_props.kobj_topology,
  665. &sys_props.attr_props);
  666. if (sys_props.kobj_nodes) {
  667. kobject_del(sys_props.kobj_nodes);
  668. kobject_put(sys_props.kobj_nodes);
  669. sys_props.kobj_nodes = NULL;
  670. }
  671. kobject_del(sys_props.kobj_topology);
  672. kobject_put(sys_props.kobj_topology);
  673. sys_props.kobj_topology = NULL;
  674. }
  675. }
  676. /* Called with write topology_lock acquired */
  677. static void kfd_topology_update_device_list(struct list_head *temp_list,
  678. struct list_head *master_list)
  679. {
  680. while (!list_empty(temp_list)) {
  681. list_move_tail(temp_list->next, master_list);
  682. sys_props.num_devices++;
  683. }
  684. }
  685. static void kfd_debug_print_topology(void)
  686. {
  687. struct kfd_topology_device *dev;
  688. down_read(&topology_lock);
  689. dev = list_last_entry(&topology_device_list,
  690. struct kfd_topology_device, list);
  691. if (dev) {
  692. if (dev->node_props.cpu_cores_count &&
  693. dev->node_props.simd_count) {
  694. pr_info("Topology: Add APU node [0x%0x:0x%0x]\n",
  695. dev->node_props.device_id,
  696. dev->node_props.vendor_id);
  697. } else if (dev->node_props.cpu_cores_count)
  698. pr_info("Topology: Add CPU node\n");
  699. else if (dev->node_props.simd_count)
  700. pr_info("Topology: Add dGPU node [0x%0x:0x%0x]\n",
  701. dev->node_props.device_id,
  702. dev->node_props.vendor_id);
  703. }
  704. up_read(&topology_lock);
  705. }
  706. /* Helper function for intializing platform_xx members of
  707. * kfd_system_properties. Uses OEM info from the last CPU/APU node.
  708. */
  709. static void kfd_update_system_properties(void)
  710. {
  711. struct kfd_topology_device *dev;
  712. down_read(&topology_lock);
  713. dev = list_last_entry(&topology_device_list,
  714. struct kfd_topology_device, list);
  715. if (dev) {
  716. sys_props.platform_id =
  717. (*((uint64_t *)dev->oem_id)) & CRAT_OEMID_64BIT_MASK;
  718. sys_props.platform_oem = *((uint64_t *)dev->oem_table_id);
  719. sys_props.platform_rev = dev->oem_revision;
  720. }
  721. up_read(&topology_lock);
  722. }
  723. static void find_system_memory(const struct dmi_header *dm,
  724. void *private)
  725. {
  726. struct kfd_mem_properties *mem;
  727. u16 mem_width, mem_clock;
  728. struct kfd_topology_device *kdev =
  729. (struct kfd_topology_device *)private;
  730. const u8 *dmi_data = (const u8 *)(dm + 1);
  731. if (dm->type == DMI_ENTRY_MEM_DEVICE && dm->length >= 0x15) {
  732. mem_width = (u16)(*(const u16 *)(dmi_data + 0x6));
  733. mem_clock = (u16)(*(const u16 *)(dmi_data + 0x11));
  734. list_for_each_entry(mem, &kdev->mem_props, list) {
  735. if (mem_width != 0xFFFF && mem_width != 0)
  736. mem->width = mem_width;
  737. if (mem_clock != 0)
  738. mem->mem_clk_max = mem_clock;
  739. }
  740. }
  741. }
  742. /*
  743. * Performance counters information is not part of CRAT but we would like to
  744. * put them in the sysfs under topology directory for Thunk to get the data.
  745. * This function is called before updating the sysfs.
  746. */
  747. static int kfd_add_perf_to_topology(struct kfd_topology_device *kdev)
  748. {
  749. struct kfd_perf_properties *props;
  750. if (amd_iommu_pc_supported()) {
  751. props = kfd_alloc_struct(props);
  752. if (!props)
  753. return -ENOMEM;
  754. strcpy(props->block_name, "iommu");
  755. props->max_concurrent = amd_iommu_pc_get_max_banks(0) *
  756. amd_iommu_pc_get_max_counters(0); /* assume one iommu */
  757. list_add_tail(&props->list, &kdev->perf_props);
  758. }
  759. return 0;
  760. }
  761. /* kfd_add_non_crat_information - Add information that is not currently
  762. * defined in CRAT but is necessary for KFD topology
  763. * @dev - topology device to which addition info is added
  764. */
  765. static void kfd_add_non_crat_information(struct kfd_topology_device *kdev)
  766. {
  767. /* Check if CPU only node. */
  768. if (!kdev->gpu) {
  769. /* Add system memory information */
  770. dmi_walk(find_system_memory, kdev);
  771. }
  772. /* TODO: For GPU node, rearrange code from kfd_topology_add_device */
  773. }
  774. /* kfd_is_acpi_crat_invalid - CRAT from ACPI is valid only for AMD APU devices.
  775. * Ignore CRAT for all other devices. AMD APU is identified if both CPU
  776. * and GPU cores are present.
  777. * @device_list - topology device list created by parsing ACPI CRAT table.
  778. * @return - TRUE if invalid, FALSE is valid.
  779. */
  780. static bool kfd_is_acpi_crat_invalid(struct list_head *device_list)
  781. {
  782. struct kfd_topology_device *dev;
  783. list_for_each_entry(dev, device_list, list) {
  784. if (dev->node_props.cpu_cores_count &&
  785. dev->node_props.simd_count)
  786. return false;
  787. }
  788. pr_info("Ignoring ACPI CRAT on non-APU system\n");
  789. return true;
  790. }
  791. int kfd_topology_init(void)
  792. {
  793. void *crat_image = NULL;
  794. size_t image_size = 0;
  795. int ret;
  796. struct list_head temp_topology_device_list;
  797. int cpu_only_node = 0;
  798. struct kfd_topology_device *kdev;
  799. int proximity_domain;
  800. /* topology_device_list - Master list of all topology devices
  801. * temp_topology_device_list - temporary list created while parsing CRAT
  802. * or VCRAT. Once parsing is complete the contents of list is moved to
  803. * topology_device_list
  804. */
  805. /* Initialize the head for the both the lists */
  806. INIT_LIST_HEAD(&topology_device_list);
  807. INIT_LIST_HEAD(&temp_topology_device_list);
  808. init_rwsem(&topology_lock);
  809. memset(&sys_props, 0, sizeof(sys_props));
  810. /* Proximity domains in ACPI CRAT tables start counting at
  811. * 0. The same should be true for virtual CRAT tables created
  812. * at this stage. GPUs added later in kfd_topology_add_device
  813. * use a counter.
  814. */
  815. proximity_domain = 0;
  816. /*
  817. * Get the CRAT image from the ACPI. If ACPI doesn't have one
  818. * or if ACPI CRAT is invalid create a virtual CRAT.
  819. * NOTE: The current implementation expects all AMD APUs to have
  820. * CRAT. If no CRAT is available, it is assumed to be a CPU
  821. */
  822. ret = kfd_create_crat_image_acpi(&crat_image, &image_size);
  823. if (!ret) {
  824. ret = kfd_parse_crat_table(crat_image,
  825. &temp_topology_device_list,
  826. proximity_domain);
  827. if (ret ||
  828. kfd_is_acpi_crat_invalid(&temp_topology_device_list)) {
  829. kfd_release_topology_device_list(
  830. &temp_topology_device_list);
  831. kfd_destroy_crat_image(crat_image);
  832. crat_image = NULL;
  833. }
  834. }
  835. if (!crat_image) {
  836. ret = kfd_create_crat_image_virtual(&crat_image, &image_size,
  837. COMPUTE_UNIT_CPU, NULL,
  838. proximity_domain);
  839. cpu_only_node = 1;
  840. if (ret) {
  841. pr_err("Error creating VCRAT table for CPU\n");
  842. return ret;
  843. }
  844. ret = kfd_parse_crat_table(crat_image,
  845. &temp_topology_device_list,
  846. proximity_domain);
  847. if (ret) {
  848. pr_err("Error parsing VCRAT table for CPU\n");
  849. goto err;
  850. }
  851. }
  852. kdev = list_first_entry(&temp_topology_device_list,
  853. struct kfd_topology_device, list);
  854. kfd_add_perf_to_topology(kdev);
  855. down_write(&topology_lock);
  856. kfd_topology_update_device_list(&temp_topology_device_list,
  857. &topology_device_list);
  858. atomic_set(&topology_crat_proximity_domain, sys_props.num_devices-1);
  859. ret = kfd_topology_update_sysfs();
  860. up_write(&topology_lock);
  861. if (!ret) {
  862. sys_props.generation_count++;
  863. kfd_update_system_properties();
  864. kfd_debug_print_topology();
  865. pr_info("Finished initializing topology\n");
  866. } else
  867. pr_err("Failed to update topology in sysfs ret=%d\n", ret);
  868. /* For nodes with GPU, this information gets added
  869. * when GPU is detected (kfd_topology_add_device).
  870. */
  871. if (cpu_only_node) {
  872. /* Add additional information to CPU only node created above */
  873. down_write(&topology_lock);
  874. kdev = list_first_entry(&topology_device_list,
  875. struct kfd_topology_device, list);
  876. up_write(&topology_lock);
  877. kfd_add_non_crat_information(kdev);
  878. }
  879. err:
  880. kfd_destroy_crat_image(crat_image);
  881. return ret;
  882. }
  883. void kfd_topology_shutdown(void)
  884. {
  885. down_write(&topology_lock);
  886. kfd_topology_release_sysfs();
  887. kfd_release_live_view();
  888. up_write(&topology_lock);
  889. }
  890. static uint32_t kfd_generate_gpu_id(struct kfd_dev *gpu)
  891. {
  892. uint32_t hashout;
  893. uint32_t buf[7];
  894. uint64_t local_mem_size;
  895. int i;
  896. struct kfd_local_mem_info local_mem_info;
  897. if (!gpu)
  898. return 0;
  899. gpu->kfd2kgd->get_local_mem_info(gpu->kgd, &local_mem_info);
  900. local_mem_size = local_mem_info.local_mem_size_private +
  901. local_mem_info.local_mem_size_public;
  902. buf[0] = gpu->pdev->devfn;
  903. buf[1] = gpu->pdev->subsystem_vendor;
  904. buf[2] = gpu->pdev->subsystem_device;
  905. buf[3] = gpu->pdev->device;
  906. buf[4] = gpu->pdev->bus->number;
  907. buf[5] = lower_32_bits(local_mem_size);
  908. buf[6] = upper_32_bits(local_mem_size);
  909. for (i = 0, hashout = 0; i < 7; i++)
  910. hashout ^= hash_32(buf[i], KFD_GPU_ID_HASH_WIDTH);
  911. return hashout;
  912. }
  913. /* kfd_assign_gpu - Attach @gpu to the correct kfd topology device. If
  914. * the GPU device is not already present in the topology device
  915. * list then return NULL. This means a new topology device has to
  916. * be created for this GPU.
  917. * TODO: Rather than assiging @gpu to first topology device withtout
  918. * gpu attached, it will better to have more stringent check.
  919. */
  920. static struct kfd_topology_device *kfd_assign_gpu(struct kfd_dev *gpu)
  921. {
  922. struct kfd_topology_device *dev;
  923. struct kfd_topology_device *out_dev = NULL;
  924. down_write(&topology_lock);
  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. up_write(&topology_lock);
  932. return out_dev;
  933. }
  934. static void kfd_notify_gpu_change(uint32_t gpu_id, int arrival)
  935. {
  936. /*
  937. * TODO: Generate an event for thunk about the arrival/removal
  938. * of the GPU
  939. */
  940. }
  941. /* kfd_fill_mem_clk_max_info - Since CRAT doesn't have memory clock info,
  942. * patch this after CRAT parsing.
  943. */
  944. static void kfd_fill_mem_clk_max_info(struct kfd_topology_device *dev)
  945. {
  946. struct kfd_mem_properties *mem;
  947. struct kfd_local_mem_info local_mem_info;
  948. if (!dev)
  949. return;
  950. /* Currently, amdgpu driver (amdgpu_mc) deals only with GPUs with
  951. * single bank of VRAM local memory.
  952. * for dGPUs - VCRAT reports only one bank of Local Memory
  953. * for APUs - If CRAT from ACPI reports more than one bank, then
  954. * all the banks will report the same mem_clk_max information
  955. */
  956. dev->gpu->kfd2kgd->get_local_mem_info(dev->gpu->kgd,
  957. &local_mem_info);
  958. list_for_each_entry(mem, &dev->mem_props, list)
  959. mem->mem_clk_max = local_mem_info.mem_clk_max;
  960. }
  961. static void kfd_fill_iolink_non_crat_info(struct kfd_topology_device *dev)
  962. {
  963. struct kfd_iolink_properties *link;
  964. if (!dev || !dev->gpu)
  965. return;
  966. /* GPU only creates direck links so apply flags setting to all */
  967. if (dev->gpu->device_info->asic_family == CHIP_HAWAII)
  968. list_for_each_entry(link, &dev->io_link_props, list)
  969. link->flags = CRAT_IOLINK_FLAGS_ENABLED |
  970. CRAT_IOLINK_FLAGS_NO_ATOMICS_32_BIT |
  971. CRAT_IOLINK_FLAGS_NO_ATOMICS_64_BIT;
  972. }
  973. int kfd_topology_add_device(struct kfd_dev *gpu)
  974. {
  975. uint32_t gpu_id;
  976. struct kfd_topology_device *dev;
  977. struct kfd_cu_info cu_info;
  978. int res = 0;
  979. struct list_head temp_topology_device_list;
  980. void *crat_image = NULL;
  981. size_t image_size = 0;
  982. int proximity_domain;
  983. INIT_LIST_HEAD(&temp_topology_device_list);
  984. gpu_id = kfd_generate_gpu_id(gpu);
  985. pr_debug("Adding new GPU (ID: 0x%x) to topology\n", gpu_id);
  986. proximity_domain = atomic_inc_return(&topology_crat_proximity_domain);
  987. /* Check to see if this gpu device exists in the topology_device_list.
  988. * If so, assign the gpu to that device,
  989. * else create a Virtual CRAT for this gpu device and then parse that
  990. * CRAT to create a new topology device. Once created assign the gpu to
  991. * that topology device
  992. */
  993. dev = kfd_assign_gpu(gpu);
  994. if (!dev) {
  995. res = kfd_create_crat_image_virtual(&crat_image, &image_size,
  996. COMPUTE_UNIT_GPU, gpu,
  997. proximity_domain);
  998. if (res) {
  999. pr_err("Error creating VCRAT for GPU (ID: 0x%x)\n",
  1000. gpu_id);
  1001. return res;
  1002. }
  1003. res = kfd_parse_crat_table(crat_image,
  1004. &temp_topology_device_list,
  1005. proximity_domain);
  1006. if (res) {
  1007. pr_err("Error parsing VCRAT for GPU (ID: 0x%x)\n",
  1008. gpu_id);
  1009. goto err;
  1010. }
  1011. down_write(&topology_lock);
  1012. kfd_topology_update_device_list(&temp_topology_device_list,
  1013. &topology_device_list);
  1014. /* Update the SYSFS tree, since we added another topology
  1015. * device
  1016. */
  1017. res = kfd_topology_update_sysfs();
  1018. up_write(&topology_lock);
  1019. if (!res)
  1020. sys_props.generation_count++;
  1021. else
  1022. pr_err("Failed to update GPU (ID: 0x%x) to sysfs topology. res=%d\n",
  1023. gpu_id, res);
  1024. dev = kfd_assign_gpu(gpu);
  1025. if (WARN_ON(!dev)) {
  1026. res = -ENODEV;
  1027. goto err;
  1028. }
  1029. }
  1030. dev->gpu_id = gpu_id;
  1031. gpu->id = gpu_id;
  1032. /* TODO: Move the following lines to function
  1033. * kfd_add_non_crat_information
  1034. */
  1035. /* Fill-in additional information that is not available in CRAT but
  1036. * needed for the topology
  1037. */
  1038. dev->gpu->kfd2kgd->get_cu_info(dev->gpu->kgd, &cu_info);
  1039. dev->node_props.simd_arrays_per_engine =
  1040. cu_info.num_shader_arrays_per_engine;
  1041. dev->node_props.vendor_id = gpu->pdev->vendor;
  1042. dev->node_props.device_id = gpu->pdev->device;
  1043. dev->node_props.location_id = PCI_DEVID(gpu->pdev->bus->number,
  1044. gpu->pdev->devfn);
  1045. dev->node_props.max_engine_clk_fcompute =
  1046. dev->gpu->kfd2kgd->get_max_engine_clock_in_mhz(dev->gpu->kgd);
  1047. dev->node_props.max_engine_clk_ccompute =
  1048. cpufreq_quick_get_max(0) / 1000;
  1049. kfd_fill_mem_clk_max_info(dev);
  1050. kfd_fill_iolink_non_crat_info(dev);
  1051. switch (dev->gpu->device_info->asic_family) {
  1052. case CHIP_KAVERI:
  1053. case CHIP_HAWAII:
  1054. case CHIP_TONGA:
  1055. dev->node_props.capability |= ((HSA_CAP_DOORBELL_TYPE_PRE_1_0 <<
  1056. HSA_CAP_DOORBELL_TYPE_TOTALBITS_SHIFT) &
  1057. HSA_CAP_DOORBELL_TYPE_TOTALBITS_MASK);
  1058. break;
  1059. case CHIP_CARRIZO:
  1060. case CHIP_FIJI:
  1061. case CHIP_POLARIS10:
  1062. case CHIP_POLARIS11:
  1063. pr_debug("Adding doorbell packet type capability\n");
  1064. dev->node_props.capability |= ((HSA_CAP_DOORBELL_TYPE_1_0 <<
  1065. HSA_CAP_DOORBELL_TYPE_TOTALBITS_SHIFT) &
  1066. HSA_CAP_DOORBELL_TYPE_TOTALBITS_MASK);
  1067. break;
  1068. default:
  1069. WARN(1, "Unexpected ASIC family %u",
  1070. dev->gpu->device_info->asic_family);
  1071. }
  1072. /* Fix errors in CZ CRAT.
  1073. * simd_count: Carrizo CRAT reports wrong simd_count, probably
  1074. * because it doesn't consider masked out CUs
  1075. * max_waves_per_simd: Carrizo reports wrong max_waves_per_simd
  1076. * capability flag: Carrizo CRAT doesn't report IOMMU flags
  1077. */
  1078. if (dev->gpu->device_info->asic_family == CHIP_CARRIZO) {
  1079. dev->node_props.simd_count =
  1080. cu_info.simd_per_cu * cu_info.cu_active_number;
  1081. dev->node_props.max_waves_per_simd = 10;
  1082. dev->node_props.capability |= HSA_CAP_ATS_PRESENT;
  1083. }
  1084. kfd_debug_print_topology();
  1085. if (!res)
  1086. kfd_notify_gpu_change(gpu_id, 1);
  1087. err:
  1088. kfd_destroy_crat_image(crat_image);
  1089. return res;
  1090. }
  1091. int kfd_topology_remove_device(struct kfd_dev *gpu)
  1092. {
  1093. struct kfd_topology_device *dev, *tmp;
  1094. uint32_t gpu_id;
  1095. int res = -ENODEV;
  1096. down_write(&topology_lock);
  1097. list_for_each_entry_safe(dev, tmp, &topology_device_list, list)
  1098. if (dev->gpu == gpu) {
  1099. gpu_id = dev->gpu_id;
  1100. kfd_remove_sysfs_node_entry(dev);
  1101. kfd_release_topology_device(dev);
  1102. sys_props.num_devices--;
  1103. res = 0;
  1104. if (kfd_topology_update_sysfs() < 0)
  1105. kfd_topology_release_sysfs();
  1106. break;
  1107. }
  1108. up_write(&topology_lock);
  1109. if (!res)
  1110. kfd_notify_gpu_change(gpu_id, 0);
  1111. return res;
  1112. }
  1113. /* kfd_topology_enum_kfd_devices - Enumerate through all devices in KFD
  1114. * topology. If GPU device is found @idx, then valid kfd_dev pointer is
  1115. * returned through @kdev
  1116. * Return - 0: On success (@kdev will be NULL for non GPU nodes)
  1117. * -1: If end of list
  1118. */
  1119. int kfd_topology_enum_kfd_devices(uint8_t idx, struct kfd_dev **kdev)
  1120. {
  1121. struct kfd_topology_device *top_dev;
  1122. uint8_t device_idx = 0;
  1123. *kdev = NULL;
  1124. down_read(&topology_lock);
  1125. list_for_each_entry(top_dev, &topology_device_list, list) {
  1126. if (device_idx == idx) {
  1127. *kdev = top_dev->gpu;
  1128. up_read(&topology_lock);
  1129. return 0;
  1130. }
  1131. device_idx++;
  1132. }
  1133. up_read(&topology_lock);
  1134. return -1;
  1135. }
  1136. static int kfd_cpumask_to_apic_id(const struct cpumask *cpumask)
  1137. {
  1138. const struct cpuinfo_x86 *cpuinfo;
  1139. int first_cpu_of_numa_node;
  1140. if (!cpumask || cpumask == cpu_none_mask)
  1141. return -1;
  1142. first_cpu_of_numa_node = cpumask_first(cpumask);
  1143. if (first_cpu_of_numa_node >= nr_cpu_ids)
  1144. return -1;
  1145. cpuinfo = &cpu_data(first_cpu_of_numa_node);
  1146. return cpuinfo->apicid;
  1147. }
  1148. /* kfd_numa_node_to_apic_id - Returns the APIC ID of the first logical processor
  1149. * of the given NUMA node (numa_node_id)
  1150. * Return -1 on failure
  1151. */
  1152. int kfd_numa_node_to_apic_id(int numa_node_id)
  1153. {
  1154. if (numa_node_id == -1) {
  1155. pr_warn("Invalid NUMA Node. Use online CPU mask\n");
  1156. return kfd_cpumask_to_apic_id(cpu_online_mask);
  1157. }
  1158. return kfd_cpumask_to_apic_id(cpumask_of_node(numa_node_id));
  1159. }
  1160. #if defined(CONFIG_DEBUG_FS)
  1161. int kfd_debugfs_hqds_by_device(struct seq_file *m, void *data)
  1162. {
  1163. struct kfd_topology_device *dev;
  1164. unsigned int i = 0;
  1165. int r = 0;
  1166. down_read(&topology_lock);
  1167. list_for_each_entry(dev, &topology_device_list, list) {
  1168. if (!dev->gpu) {
  1169. i++;
  1170. continue;
  1171. }
  1172. seq_printf(m, "Node %u, gpu_id %x:\n", i++, dev->gpu->id);
  1173. r = dqm_debugfs_hqds(m, dev->gpu->dqm);
  1174. if (r)
  1175. break;
  1176. }
  1177. up_read(&topology_lock);
  1178. return r;
  1179. }
  1180. int kfd_debugfs_rls_by_device(struct seq_file *m, void *data)
  1181. {
  1182. struct kfd_topology_device *dev;
  1183. unsigned int i = 0;
  1184. int r = 0;
  1185. down_read(&topology_lock);
  1186. list_for_each_entry(dev, &topology_device_list, list) {
  1187. if (!dev->gpu) {
  1188. i++;
  1189. continue;
  1190. }
  1191. seq_printf(m, "Node %u, gpu_id %x:\n", i++, dev->gpu->id);
  1192. r = pm_debugfs_runlist(m, &dev->gpu->dqm->packets);
  1193. if (r)
  1194. break;
  1195. }
  1196. up_read(&topology_lock);
  1197. return r;
  1198. }
  1199. #endif