kfd_topology.c 38 KB

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