kfd_topology.c 38 KB

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