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