kfd_topology.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390
  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. sysfs_show_32bit_prop(buffer, "max_engine_clk_fcompute",
  384. dev->node_props.max_engine_clk_fcompute);
  385. sysfs_show_64bit_prop(buffer, "local_mem_size",
  386. (unsigned long long int) 0);
  387. sysfs_show_32bit_prop(buffer, "fw_version",
  388. dev->gpu->kfd2kgd->get_fw_version(
  389. dev->gpu->kgd,
  390. KGD_ENGINE_MEC1));
  391. sysfs_show_32bit_prop(buffer, "capability",
  392. dev->node_props.capability);
  393. }
  394. return sysfs_show_32bit_prop(buffer, "max_engine_clk_ccompute",
  395. cpufreq_quick_get_max(0)/1000);
  396. }
  397. static const struct sysfs_ops node_ops = {
  398. .show = node_show,
  399. };
  400. static struct kobj_type node_type = {
  401. .release = kfd_topology_kobj_release,
  402. .sysfs_ops = &node_ops,
  403. };
  404. static void kfd_remove_sysfs_file(struct kobject *kobj, struct attribute *attr)
  405. {
  406. sysfs_remove_file(kobj, attr);
  407. kobject_del(kobj);
  408. kobject_put(kobj);
  409. }
  410. static void kfd_remove_sysfs_node_entry(struct kfd_topology_device *dev)
  411. {
  412. struct kfd_iolink_properties *iolink;
  413. struct kfd_cache_properties *cache;
  414. struct kfd_mem_properties *mem;
  415. struct kfd_perf_properties *perf;
  416. if (dev->kobj_iolink) {
  417. list_for_each_entry(iolink, &dev->io_link_props, list)
  418. if (iolink->kobj) {
  419. kfd_remove_sysfs_file(iolink->kobj,
  420. &iolink->attr);
  421. iolink->kobj = NULL;
  422. }
  423. kobject_del(dev->kobj_iolink);
  424. kobject_put(dev->kobj_iolink);
  425. dev->kobj_iolink = NULL;
  426. }
  427. if (dev->kobj_cache) {
  428. list_for_each_entry(cache, &dev->cache_props, list)
  429. if (cache->kobj) {
  430. kfd_remove_sysfs_file(cache->kobj,
  431. &cache->attr);
  432. cache->kobj = NULL;
  433. }
  434. kobject_del(dev->kobj_cache);
  435. kobject_put(dev->kobj_cache);
  436. dev->kobj_cache = NULL;
  437. }
  438. if (dev->kobj_mem) {
  439. list_for_each_entry(mem, &dev->mem_props, list)
  440. if (mem->kobj) {
  441. kfd_remove_sysfs_file(mem->kobj, &mem->attr);
  442. mem->kobj = NULL;
  443. }
  444. kobject_del(dev->kobj_mem);
  445. kobject_put(dev->kobj_mem);
  446. dev->kobj_mem = NULL;
  447. }
  448. if (dev->kobj_perf) {
  449. list_for_each_entry(perf, &dev->perf_props, list) {
  450. kfree(perf->attr_group);
  451. perf->attr_group = NULL;
  452. }
  453. kobject_del(dev->kobj_perf);
  454. kobject_put(dev->kobj_perf);
  455. dev->kobj_perf = NULL;
  456. }
  457. if (dev->kobj_node) {
  458. sysfs_remove_file(dev->kobj_node, &dev->attr_gpuid);
  459. sysfs_remove_file(dev->kobj_node, &dev->attr_name);
  460. sysfs_remove_file(dev->kobj_node, &dev->attr_props);
  461. kobject_del(dev->kobj_node);
  462. kobject_put(dev->kobj_node);
  463. dev->kobj_node = NULL;
  464. }
  465. }
  466. static int kfd_build_sysfs_node_entry(struct kfd_topology_device *dev,
  467. uint32_t id)
  468. {
  469. struct kfd_iolink_properties *iolink;
  470. struct kfd_cache_properties *cache;
  471. struct kfd_mem_properties *mem;
  472. struct kfd_perf_properties *perf;
  473. int ret;
  474. uint32_t i, num_attrs;
  475. struct attribute **attrs;
  476. if (WARN_ON(dev->kobj_node))
  477. return -EEXIST;
  478. /*
  479. * Creating the sysfs folders
  480. */
  481. dev->kobj_node = kfd_alloc_struct(dev->kobj_node);
  482. if (!dev->kobj_node)
  483. return -ENOMEM;
  484. ret = kobject_init_and_add(dev->kobj_node, &node_type,
  485. sys_props.kobj_nodes, "%d", id);
  486. if (ret < 0)
  487. return ret;
  488. dev->kobj_mem = kobject_create_and_add("mem_banks", dev->kobj_node);
  489. if (!dev->kobj_mem)
  490. return -ENOMEM;
  491. dev->kobj_cache = kobject_create_and_add("caches", dev->kobj_node);
  492. if (!dev->kobj_cache)
  493. return -ENOMEM;
  494. dev->kobj_iolink = kobject_create_and_add("io_links", dev->kobj_node);
  495. if (!dev->kobj_iolink)
  496. return -ENOMEM;
  497. dev->kobj_perf = kobject_create_and_add("perf", dev->kobj_node);
  498. if (!dev->kobj_perf)
  499. return -ENOMEM;
  500. /*
  501. * Creating sysfs files for node properties
  502. */
  503. dev->attr_gpuid.name = "gpu_id";
  504. dev->attr_gpuid.mode = KFD_SYSFS_FILE_MODE;
  505. sysfs_attr_init(&dev->attr_gpuid);
  506. dev->attr_name.name = "name";
  507. dev->attr_name.mode = KFD_SYSFS_FILE_MODE;
  508. sysfs_attr_init(&dev->attr_name);
  509. dev->attr_props.name = "properties";
  510. dev->attr_props.mode = KFD_SYSFS_FILE_MODE;
  511. sysfs_attr_init(&dev->attr_props);
  512. ret = sysfs_create_file(dev->kobj_node, &dev->attr_gpuid);
  513. if (ret < 0)
  514. return ret;
  515. ret = sysfs_create_file(dev->kobj_node, &dev->attr_name);
  516. if (ret < 0)
  517. return ret;
  518. ret = sysfs_create_file(dev->kobj_node, &dev->attr_props);
  519. if (ret < 0)
  520. return ret;
  521. i = 0;
  522. list_for_each_entry(mem, &dev->mem_props, list) {
  523. mem->kobj = kzalloc(sizeof(struct kobject), GFP_KERNEL);
  524. if (!mem->kobj)
  525. return -ENOMEM;
  526. ret = kobject_init_and_add(mem->kobj, &mem_type,
  527. dev->kobj_mem, "%d", i);
  528. if (ret < 0)
  529. return ret;
  530. mem->attr.name = "properties";
  531. mem->attr.mode = KFD_SYSFS_FILE_MODE;
  532. sysfs_attr_init(&mem->attr);
  533. ret = sysfs_create_file(mem->kobj, &mem->attr);
  534. if (ret < 0)
  535. return ret;
  536. i++;
  537. }
  538. i = 0;
  539. list_for_each_entry(cache, &dev->cache_props, list) {
  540. cache->kobj = kzalloc(sizeof(struct kobject), GFP_KERNEL);
  541. if (!cache->kobj)
  542. return -ENOMEM;
  543. ret = kobject_init_and_add(cache->kobj, &cache_type,
  544. dev->kobj_cache, "%d", i);
  545. if (ret < 0)
  546. return ret;
  547. cache->attr.name = "properties";
  548. cache->attr.mode = KFD_SYSFS_FILE_MODE;
  549. sysfs_attr_init(&cache->attr);
  550. ret = sysfs_create_file(cache->kobj, &cache->attr);
  551. if (ret < 0)
  552. return ret;
  553. i++;
  554. }
  555. i = 0;
  556. list_for_each_entry(iolink, &dev->io_link_props, list) {
  557. iolink->kobj = kzalloc(sizeof(struct kobject), GFP_KERNEL);
  558. if (!iolink->kobj)
  559. return -ENOMEM;
  560. ret = kobject_init_and_add(iolink->kobj, &iolink_type,
  561. dev->kobj_iolink, "%d", i);
  562. if (ret < 0)
  563. return ret;
  564. iolink->attr.name = "properties";
  565. iolink->attr.mode = KFD_SYSFS_FILE_MODE;
  566. sysfs_attr_init(&iolink->attr);
  567. ret = sysfs_create_file(iolink->kobj, &iolink->attr);
  568. if (ret < 0)
  569. return ret;
  570. i++;
  571. }
  572. /* All hardware blocks have the same number of attributes. */
  573. num_attrs = sizeof(perf_attr_iommu)/sizeof(struct kfd_perf_attr);
  574. list_for_each_entry(perf, &dev->perf_props, list) {
  575. perf->attr_group = kzalloc(sizeof(struct kfd_perf_attr)
  576. * num_attrs + sizeof(struct attribute_group),
  577. GFP_KERNEL);
  578. if (!perf->attr_group)
  579. return -ENOMEM;
  580. attrs = (struct attribute **)(perf->attr_group + 1);
  581. if (!strcmp(perf->block_name, "iommu")) {
  582. /* Information of IOMMU's num_counters and counter_ids is shown
  583. * under /sys/bus/event_source/devices/amd_iommu. We don't
  584. * duplicate here.
  585. */
  586. perf_attr_iommu[0].data = perf->max_concurrent;
  587. for (i = 0; i < num_attrs; i++)
  588. attrs[i] = &perf_attr_iommu[i].attr.attr;
  589. }
  590. perf->attr_group->name = perf->block_name;
  591. perf->attr_group->attrs = attrs;
  592. ret = sysfs_create_group(dev->kobj_perf, perf->attr_group);
  593. if (ret < 0)
  594. return ret;
  595. }
  596. return 0;
  597. }
  598. /* Called with write topology lock acquired */
  599. static int kfd_build_sysfs_node_tree(void)
  600. {
  601. struct kfd_topology_device *dev;
  602. int ret;
  603. uint32_t i = 0;
  604. list_for_each_entry(dev, &topology_device_list, list) {
  605. ret = kfd_build_sysfs_node_entry(dev, i);
  606. if (ret < 0)
  607. return ret;
  608. i++;
  609. }
  610. return 0;
  611. }
  612. /* Called with write topology lock acquired */
  613. static void kfd_remove_sysfs_node_tree(void)
  614. {
  615. struct kfd_topology_device *dev;
  616. list_for_each_entry(dev, &topology_device_list, list)
  617. kfd_remove_sysfs_node_entry(dev);
  618. }
  619. static int kfd_topology_update_sysfs(void)
  620. {
  621. int ret;
  622. pr_info("Creating topology SYSFS entries\n");
  623. if (!sys_props.kobj_topology) {
  624. sys_props.kobj_topology =
  625. kfd_alloc_struct(sys_props.kobj_topology);
  626. if (!sys_props.kobj_topology)
  627. return -ENOMEM;
  628. ret = kobject_init_and_add(sys_props.kobj_topology,
  629. &sysprops_type, &kfd_device->kobj,
  630. "topology");
  631. if (ret < 0)
  632. return ret;
  633. sys_props.kobj_nodes = kobject_create_and_add("nodes",
  634. sys_props.kobj_topology);
  635. if (!sys_props.kobj_nodes)
  636. return -ENOMEM;
  637. sys_props.attr_genid.name = "generation_id";
  638. sys_props.attr_genid.mode = KFD_SYSFS_FILE_MODE;
  639. sysfs_attr_init(&sys_props.attr_genid);
  640. ret = sysfs_create_file(sys_props.kobj_topology,
  641. &sys_props.attr_genid);
  642. if (ret < 0)
  643. return ret;
  644. sys_props.attr_props.name = "system_properties";
  645. sys_props.attr_props.mode = KFD_SYSFS_FILE_MODE;
  646. sysfs_attr_init(&sys_props.attr_props);
  647. ret = sysfs_create_file(sys_props.kobj_topology,
  648. &sys_props.attr_props);
  649. if (ret < 0)
  650. return ret;
  651. }
  652. kfd_remove_sysfs_node_tree();
  653. return kfd_build_sysfs_node_tree();
  654. }
  655. static void kfd_topology_release_sysfs(void)
  656. {
  657. kfd_remove_sysfs_node_tree();
  658. if (sys_props.kobj_topology) {
  659. sysfs_remove_file(sys_props.kobj_topology,
  660. &sys_props.attr_genid);
  661. sysfs_remove_file(sys_props.kobj_topology,
  662. &sys_props.attr_props);
  663. if (sys_props.kobj_nodes) {
  664. kobject_del(sys_props.kobj_nodes);
  665. kobject_put(sys_props.kobj_nodes);
  666. sys_props.kobj_nodes = NULL;
  667. }
  668. kobject_del(sys_props.kobj_topology);
  669. kobject_put(sys_props.kobj_topology);
  670. sys_props.kobj_topology = NULL;
  671. }
  672. }
  673. /* Called with write topology_lock acquired */
  674. static void kfd_topology_update_device_list(struct list_head *temp_list,
  675. struct list_head *master_list)
  676. {
  677. while (!list_empty(temp_list)) {
  678. list_move_tail(temp_list->next, master_list);
  679. sys_props.num_devices++;
  680. }
  681. }
  682. static void kfd_debug_print_topology(void)
  683. {
  684. struct kfd_topology_device *dev;
  685. down_read(&topology_lock);
  686. dev = list_last_entry(&topology_device_list,
  687. struct kfd_topology_device, list);
  688. if (dev) {
  689. if (dev->node_props.cpu_cores_count &&
  690. dev->node_props.simd_count) {
  691. pr_info("Topology: Add APU node [0x%0x:0x%0x]\n",
  692. dev->node_props.device_id,
  693. dev->node_props.vendor_id);
  694. } else if (dev->node_props.cpu_cores_count)
  695. pr_info("Topology: Add CPU node\n");
  696. else if (dev->node_props.simd_count)
  697. pr_info("Topology: Add dGPU node [0x%0x:0x%0x]\n",
  698. dev->node_props.device_id,
  699. dev->node_props.vendor_id);
  700. }
  701. up_read(&topology_lock);
  702. }
  703. /* Helper function for intializing platform_xx members of
  704. * kfd_system_properties. Uses OEM info from the last CPU/APU node.
  705. */
  706. static void kfd_update_system_properties(void)
  707. {
  708. struct kfd_topology_device *dev;
  709. down_read(&topology_lock);
  710. dev = list_last_entry(&topology_device_list,
  711. struct kfd_topology_device, list);
  712. if (dev) {
  713. sys_props.platform_id =
  714. (*((uint64_t *)dev->oem_id)) & CRAT_OEMID_64BIT_MASK;
  715. sys_props.platform_oem = *((uint64_t *)dev->oem_table_id);
  716. sys_props.platform_rev = dev->oem_revision;
  717. }
  718. up_read(&topology_lock);
  719. }
  720. static void find_system_memory(const struct dmi_header *dm,
  721. void *private)
  722. {
  723. struct kfd_mem_properties *mem;
  724. u16 mem_width, mem_clock;
  725. struct kfd_topology_device *kdev =
  726. (struct kfd_topology_device *)private;
  727. const u8 *dmi_data = (const u8 *)(dm + 1);
  728. if (dm->type == DMI_ENTRY_MEM_DEVICE && dm->length >= 0x15) {
  729. mem_width = (u16)(*(const u16 *)(dmi_data + 0x6));
  730. mem_clock = (u16)(*(const u16 *)(dmi_data + 0x11));
  731. list_for_each_entry(mem, &kdev->mem_props, list) {
  732. if (mem_width != 0xFFFF && mem_width != 0)
  733. mem->width = mem_width;
  734. if (mem_clock != 0)
  735. mem->mem_clk_max = mem_clock;
  736. }
  737. }
  738. }
  739. /*
  740. * Performance counters information is not part of CRAT but we would like to
  741. * put them in the sysfs under topology directory for Thunk to get the data.
  742. * This function is called before updating the sysfs.
  743. */
  744. static int kfd_add_perf_to_topology(struct kfd_topology_device *kdev)
  745. {
  746. struct kfd_perf_properties *props;
  747. if (amd_iommu_pc_supported()) {
  748. props = kfd_alloc_struct(props);
  749. if (!props)
  750. return -ENOMEM;
  751. strcpy(props->block_name, "iommu");
  752. props->max_concurrent = amd_iommu_pc_get_max_banks(0) *
  753. amd_iommu_pc_get_max_counters(0); /* assume one iommu */
  754. list_add_tail(&props->list, &kdev->perf_props);
  755. }
  756. return 0;
  757. }
  758. /* kfd_add_non_crat_information - Add information that is not currently
  759. * defined in CRAT but is necessary for KFD topology
  760. * @dev - topology device to which addition info is added
  761. */
  762. static void kfd_add_non_crat_information(struct kfd_topology_device *kdev)
  763. {
  764. /* Check if CPU only node. */
  765. if (!kdev->gpu) {
  766. /* Add system memory information */
  767. dmi_walk(find_system_memory, kdev);
  768. }
  769. /* TODO: For GPU node, rearrange code from kfd_topology_add_device */
  770. }
  771. int kfd_topology_init(void)
  772. {
  773. void *crat_image = NULL;
  774. size_t image_size = 0;
  775. int ret;
  776. struct list_head temp_topology_device_list;
  777. int cpu_only_node = 0;
  778. struct kfd_topology_device *kdev;
  779. int proximity_domain;
  780. /* topology_device_list - Master list of all topology devices
  781. * temp_topology_device_list - temporary list created while parsing CRAT
  782. * or VCRAT. Once parsing is complete the contents of list is moved to
  783. * topology_device_list
  784. */
  785. /* Initialize the head for the both the lists */
  786. INIT_LIST_HEAD(&topology_device_list);
  787. INIT_LIST_HEAD(&temp_topology_device_list);
  788. init_rwsem(&topology_lock);
  789. memset(&sys_props, 0, sizeof(sys_props));
  790. /* Proximity domains in ACPI CRAT tables start counting at
  791. * 0. The same should be true for virtual CRAT tables created
  792. * at this stage. GPUs added later in kfd_topology_add_device
  793. * use a counter.
  794. */
  795. proximity_domain = 0;
  796. /*
  797. * Get the CRAT image from the ACPI. If ACPI doesn't have one
  798. * create a virtual CRAT.
  799. * NOTE: The current implementation expects all AMD APUs to have
  800. * CRAT. If no CRAT is available, it is assumed to be a CPU
  801. */
  802. ret = kfd_create_crat_image_acpi(&crat_image, &image_size);
  803. if (!ret) {
  804. ret = kfd_parse_crat_table(crat_image,
  805. &temp_topology_device_list,
  806. proximity_domain);
  807. if (ret) {
  808. kfd_release_topology_device_list(
  809. &temp_topology_device_list);
  810. kfd_destroy_crat_image(crat_image);
  811. crat_image = NULL;
  812. }
  813. }
  814. if (!crat_image) {
  815. ret = kfd_create_crat_image_virtual(&crat_image, &image_size,
  816. COMPUTE_UNIT_CPU, NULL,
  817. proximity_domain);
  818. cpu_only_node = 1;
  819. if (ret) {
  820. pr_err("Error creating VCRAT table for CPU\n");
  821. return ret;
  822. }
  823. ret = kfd_parse_crat_table(crat_image,
  824. &temp_topology_device_list,
  825. proximity_domain);
  826. if (ret) {
  827. pr_err("Error parsing VCRAT table for CPU\n");
  828. goto err;
  829. }
  830. }
  831. kdev = list_first_entry(&temp_topology_device_list,
  832. struct kfd_topology_device, list);
  833. kfd_add_perf_to_topology(kdev);
  834. down_write(&topology_lock);
  835. kfd_topology_update_device_list(&temp_topology_device_list,
  836. &topology_device_list);
  837. atomic_set(&topology_crat_proximity_domain, sys_props.num_devices-1);
  838. ret = kfd_topology_update_sysfs();
  839. up_write(&topology_lock);
  840. if (!ret) {
  841. sys_props.generation_count++;
  842. kfd_update_system_properties();
  843. kfd_debug_print_topology();
  844. pr_info("Finished initializing topology\n");
  845. } else
  846. pr_err("Failed to update topology in sysfs ret=%d\n", ret);
  847. /* For nodes with GPU, this information gets added
  848. * when GPU is detected (kfd_topology_add_device).
  849. */
  850. if (cpu_only_node) {
  851. /* Add additional information to CPU only node created above */
  852. down_write(&topology_lock);
  853. kdev = list_first_entry(&topology_device_list,
  854. struct kfd_topology_device, list);
  855. up_write(&topology_lock);
  856. kfd_add_non_crat_information(kdev);
  857. }
  858. err:
  859. kfd_destroy_crat_image(crat_image);
  860. return ret;
  861. }
  862. void kfd_topology_shutdown(void)
  863. {
  864. down_write(&topology_lock);
  865. kfd_topology_release_sysfs();
  866. kfd_release_live_view();
  867. up_write(&topology_lock);
  868. }
  869. static uint32_t kfd_generate_gpu_id(struct kfd_dev *gpu)
  870. {
  871. uint32_t hashout;
  872. uint32_t buf[7];
  873. uint64_t local_mem_size;
  874. int i;
  875. struct kfd_local_mem_info local_mem_info;
  876. if (!gpu)
  877. return 0;
  878. gpu->kfd2kgd->get_local_mem_info(gpu->kgd, &local_mem_info);
  879. local_mem_size = local_mem_info.local_mem_size_private +
  880. local_mem_info.local_mem_size_public;
  881. buf[0] = gpu->pdev->devfn;
  882. buf[1] = gpu->pdev->subsystem_vendor;
  883. buf[2] = gpu->pdev->subsystem_device;
  884. buf[3] = gpu->pdev->device;
  885. buf[4] = gpu->pdev->bus->number;
  886. buf[5] = lower_32_bits(local_mem_size);
  887. buf[6] = upper_32_bits(local_mem_size);
  888. for (i = 0, hashout = 0; i < 7; i++)
  889. hashout ^= hash_32(buf[i], KFD_GPU_ID_HASH_WIDTH);
  890. return hashout;
  891. }
  892. /* kfd_assign_gpu - Attach @gpu to the correct kfd topology device. If
  893. * the GPU device is not already present in the topology device
  894. * list then return NULL. This means a new topology device has to
  895. * be created for this GPU.
  896. * TODO: Rather than assiging @gpu to first topology device withtout
  897. * gpu attached, it will better to have more stringent check.
  898. */
  899. static struct kfd_topology_device *kfd_assign_gpu(struct kfd_dev *gpu)
  900. {
  901. struct kfd_topology_device *dev;
  902. struct kfd_topology_device *out_dev = NULL;
  903. down_write(&topology_lock);
  904. list_for_each_entry(dev, &topology_device_list, list)
  905. if (!dev->gpu && (dev->node_props.simd_count > 0)) {
  906. dev->gpu = gpu;
  907. out_dev = dev;
  908. break;
  909. }
  910. up_write(&topology_lock);
  911. return out_dev;
  912. }
  913. static void kfd_notify_gpu_change(uint32_t gpu_id, int arrival)
  914. {
  915. /*
  916. * TODO: Generate an event for thunk about the arrival/removal
  917. * of the GPU
  918. */
  919. }
  920. /* kfd_fill_mem_clk_max_info - Since CRAT doesn't have memory clock info,
  921. * patch this after CRAT parsing.
  922. */
  923. static void kfd_fill_mem_clk_max_info(struct kfd_topology_device *dev)
  924. {
  925. struct kfd_mem_properties *mem;
  926. struct kfd_local_mem_info local_mem_info;
  927. if (!dev)
  928. return;
  929. /* Currently, amdgpu driver (amdgpu_mc) deals only with GPUs with
  930. * single bank of VRAM local memory.
  931. * for dGPUs - VCRAT reports only one bank of Local Memory
  932. * for APUs - If CRAT from ACPI reports more than one bank, then
  933. * all the banks will report the same mem_clk_max information
  934. */
  935. dev->gpu->kfd2kgd->get_local_mem_info(dev->gpu->kgd,
  936. &local_mem_info);
  937. list_for_each_entry(mem, &dev->mem_props, list)
  938. mem->mem_clk_max = local_mem_info.mem_clk_max;
  939. }
  940. static void kfd_fill_iolink_non_crat_info(struct kfd_topology_device *dev)
  941. {
  942. struct kfd_iolink_properties *link;
  943. if (!dev || !dev->gpu)
  944. return;
  945. /* GPU only creates direck links so apply flags setting to all */
  946. if (dev->gpu->device_info->asic_family == CHIP_HAWAII)
  947. list_for_each_entry(link, &dev->io_link_props, list)
  948. link->flags = CRAT_IOLINK_FLAGS_ENABLED |
  949. CRAT_IOLINK_FLAGS_NO_ATOMICS_32_BIT |
  950. CRAT_IOLINK_FLAGS_NO_ATOMICS_64_BIT;
  951. }
  952. int kfd_topology_add_device(struct kfd_dev *gpu)
  953. {
  954. uint32_t gpu_id;
  955. struct kfd_topology_device *dev;
  956. struct kfd_cu_info cu_info;
  957. int res = 0;
  958. struct list_head temp_topology_device_list;
  959. void *crat_image = NULL;
  960. size_t image_size = 0;
  961. int proximity_domain;
  962. INIT_LIST_HEAD(&temp_topology_device_list);
  963. gpu_id = kfd_generate_gpu_id(gpu);
  964. pr_debug("Adding new GPU (ID: 0x%x) to topology\n", gpu_id);
  965. proximity_domain = atomic_inc_return(&topology_crat_proximity_domain);
  966. /* Check to see if this gpu device exists in the topology_device_list.
  967. * If so, assign the gpu to that device,
  968. * else create a Virtual CRAT for this gpu device and then parse that
  969. * CRAT to create a new topology device. Once created assign the gpu to
  970. * that topology device
  971. */
  972. dev = kfd_assign_gpu(gpu);
  973. if (!dev) {
  974. res = kfd_create_crat_image_virtual(&crat_image, &image_size,
  975. COMPUTE_UNIT_GPU, gpu,
  976. proximity_domain);
  977. if (res) {
  978. pr_err("Error creating VCRAT for GPU (ID: 0x%x)\n",
  979. gpu_id);
  980. return res;
  981. }
  982. res = kfd_parse_crat_table(crat_image,
  983. &temp_topology_device_list,
  984. proximity_domain);
  985. if (res) {
  986. pr_err("Error parsing VCRAT for GPU (ID: 0x%x)\n",
  987. gpu_id);
  988. goto err;
  989. }
  990. down_write(&topology_lock);
  991. kfd_topology_update_device_list(&temp_topology_device_list,
  992. &topology_device_list);
  993. /* Update the SYSFS tree, since we added another topology
  994. * device
  995. */
  996. res = kfd_topology_update_sysfs();
  997. up_write(&topology_lock);
  998. if (!res)
  999. sys_props.generation_count++;
  1000. else
  1001. pr_err("Failed to update GPU (ID: 0x%x) to sysfs topology. res=%d\n",
  1002. gpu_id, res);
  1003. dev = kfd_assign_gpu(gpu);
  1004. if (WARN_ON(!dev)) {
  1005. res = -ENODEV;
  1006. goto err;
  1007. }
  1008. }
  1009. dev->gpu_id = gpu_id;
  1010. gpu->id = gpu_id;
  1011. /* TODO: Move the following lines to function
  1012. * kfd_add_non_crat_information
  1013. */
  1014. /* Fill-in additional information that is not available in CRAT but
  1015. * needed for the topology
  1016. */
  1017. dev->gpu->kfd2kgd->get_cu_info(dev->gpu->kgd, &cu_info);
  1018. dev->node_props.simd_arrays_per_engine =
  1019. cu_info.num_shader_arrays_per_engine;
  1020. dev->node_props.vendor_id = gpu->pdev->vendor;
  1021. dev->node_props.device_id = gpu->pdev->device;
  1022. dev->node_props.location_id = PCI_DEVID(gpu->pdev->bus->number,
  1023. gpu->pdev->devfn);
  1024. dev->node_props.max_engine_clk_fcompute =
  1025. dev->gpu->kfd2kgd->get_max_engine_clock_in_mhz(dev->gpu->kgd);
  1026. dev->node_props.max_engine_clk_ccompute =
  1027. cpufreq_quick_get_max(0) / 1000;
  1028. kfd_fill_mem_clk_max_info(dev);
  1029. kfd_fill_iolink_non_crat_info(dev);
  1030. switch (dev->gpu->device_info->asic_family) {
  1031. case CHIP_KAVERI:
  1032. case CHIP_HAWAII:
  1033. case CHIP_TONGA:
  1034. dev->node_props.capability |= ((HSA_CAP_DOORBELL_TYPE_PRE_1_0 <<
  1035. HSA_CAP_DOORBELL_TYPE_TOTALBITS_SHIFT) &
  1036. HSA_CAP_DOORBELL_TYPE_TOTALBITS_MASK);
  1037. break;
  1038. case CHIP_CARRIZO:
  1039. case CHIP_FIJI:
  1040. case CHIP_POLARIS10:
  1041. case CHIP_POLARIS11:
  1042. pr_debug("Adding doorbell packet type capability\n");
  1043. dev->node_props.capability |= ((HSA_CAP_DOORBELL_TYPE_1_0 <<
  1044. HSA_CAP_DOORBELL_TYPE_TOTALBITS_SHIFT) &
  1045. HSA_CAP_DOORBELL_TYPE_TOTALBITS_MASK);
  1046. break;
  1047. default:
  1048. WARN(1, "Unexpected ASIC family %u",
  1049. dev->gpu->device_info->asic_family);
  1050. }
  1051. /* Fix errors in CZ CRAT.
  1052. * simd_count: Carrizo CRAT reports wrong simd_count, probably
  1053. * because it doesn't consider masked out CUs
  1054. * max_waves_per_simd: Carrizo reports wrong max_waves_per_simd
  1055. * capability flag: Carrizo CRAT doesn't report IOMMU flags
  1056. */
  1057. if (dev->gpu->device_info->asic_family == CHIP_CARRIZO) {
  1058. dev->node_props.simd_count =
  1059. cu_info.simd_per_cu * cu_info.cu_active_number;
  1060. dev->node_props.max_waves_per_simd = 10;
  1061. dev->node_props.capability |= HSA_CAP_ATS_PRESENT;
  1062. }
  1063. kfd_debug_print_topology();
  1064. if (!res)
  1065. kfd_notify_gpu_change(gpu_id, 1);
  1066. err:
  1067. kfd_destroy_crat_image(crat_image);
  1068. return res;
  1069. }
  1070. int kfd_topology_remove_device(struct kfd_dev *gpu)
  1071. {
  1072. struct kfd_topology_device *dev, *tmp;
  1073. uint32_t gpu_id;
  1074. int res = -ENODEV;
  1075. down_write(&topology_lock);
  1076. list_for_each_entry_safe(dev, tmp, &topology_device_list, list)
  1077. if (dev->gpu == gpu) {
  1078. gpu_id = dev->gpu_id;
  1079. kfd_remove_sysfs_node_entry(dev);
  1080. kfd_release_topology_device(dev);
  1081. sys_props.num_devices--;
  1082. res = 0;
  1083. if (kfd_topology_update_sysfs() < 0)
  1084. kfd_topology_release_sysfs();
  1085. break;
  1086. }
  1087. up_write(&topology_lock);
  1088. if (!res)
  1089. kfd_notify_gpu_change(gpu_id, 0);
  1090. return res;
  1091. }
  1092. /* kfd_topology_enum_kfd_devices - Enumerate through all devices in KFD
  1093. * topology. If GPU device is found @idx, then valid kfd_dev pointer is
  1094. * returned through @kdev
  1095. * Return - 0: On success (@kdev will be NULL for non GPU nodes)
  1096. * -1: If end of list
  1097. */
  1098. int kfd_topology_enum_kfd_devices(uint8_t idx, struct kfd_dev **kdev)
  1099. {
  1100. struct kfd_topology_device *top_dev;
  1101. uint8_t device_idx = 0;
  1102. *kdev = NULL;
  1103. down_read(&topology_lock);
  1104. list_for_each_entry(top_dev, &topology_device_list, list) {
  1105. if (device_idx == idx) {
  1106. *kdev = top_dev->gpu;
  1107. up_read(&topology_lock);
  1108. return 0;
  1109. }
  1110. device_idx++;
  1111. }
  1112. up_read(&topology_lock);
  1113. return -1;
  1114. }
  1115. static int kfd_cpumask_to_apic_id(const struct cpumask *cpumask)
  1116. {
  1117. const struct cpuinfo_x86 *cpuinfo;
  1118. int first_cpu_of_numa_node;
  1119. if (!cpumask || cpumask == cpu_none_mask)
  1120. return -1;
  1121. first_cpu_of_numa_node = cpumask_first(cpumask);
  1122. if (first_cpu_of_numa_node >= nr_cpu_ids)
  1123. return -1;
  1124. cpuinfo = &cpu_data(first_cpu_of_numa_node);
  1125. return cpuinfo->apicid;
  1126. }
  1127. /* kfd_numa_node_to_apic_id - Returns the APIC ID of the first logical processor
  1128. * of the given NUMA node (numa_node_id)
  1129. * Return -1 on failure
  1130. */
  1131. int kfd_numa_node_to_apic_id(int numa_node_id)
  1132. {
  1133. if (numa_node_id == -1) {
  1134. pr_warn("Invalid NUMA Node. Use online CPU mask\n");
  1135. return kfd_cpumask_to_apic_id(cpu_online_mask);
  1136. }
  1137. return kfd_cpumask_to_apic_id(cpumask_of_node(numa_node_id));
  1138. }
  1139. #if defined(CONFIG_DEBUG_FS)
  1140. int kfd_debugfs_hqds_by_device(struct seq_file *m, void *data)
  1141. {
  1142. struct kfd_topology_device *dev;
  1143. unsigned int i = 0;
  1144. int r = 0;
  1145. down_read(&topology_lock);
  1146. list_for_each_entry(dev, &topology_device_list, list) {
  1147. if (!dev->gpu) {
  1148. i++;
  1149. continue;
  1150. }
  1151. seq_printf(m, "Node %u, gpu_id %x:\n", i++, dev->gpu->id);
  1152. r = dqm_debugfs_hqds(m, dev->gpu->dqm);
  1153. if (r)
  1154. break;
  1155. }
  1156. up_read(&topology_lock);
  1157. return r;
  1158. }
  1159. int kfd_debugfs_rls_by_device(struct seq_file *m, void *data)
  1160. {
  1161. struct kfd_topology_device *dev;
  1162. unsigned int i = 0;
  1163. int r = 0;
  1164. down_read(&topology_lock);
  1165. list_for_each_entry(dev, &topology_device_list, list) {
  1166. if (!dev->gpu) {
  1167. i++;
  1168. continue;
  1169. }
  1170. seq_printf(m, "Node %u, gpu_id %x:\n", i++, dev->gpu->id);
  1171. r = pm_debugfs_runlist(m, &dev->gpu->dqm->packets);
  1172. if (r)
  1173. break;
  1174. }
  1175. up_read(&topology_lock);
  1176. return r;
  1177. }
  1178. #endif