kfd_topology.c 68 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402
  1. // SPDX-License-Identifier: GPL-2.0 OR MIT
  2. /*
  3. * Copyright 2014-2022 Advanced Micro Devices, Inc.
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a
  6. * copy of this software and associated documentation files (the "Software"),
  7. * to deal in the Software without restriction, including without limitation
  8. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  9. * and/or sell copies of the Software, and to permit persons to whom the
  10. * Software is furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be included in
  13. * all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  18. * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
  19. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  20. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  21. * OTHER DEALINGS IN THE SOFTWARE.
  22. */
  23. #include <linux/types.h>
  24. #include <linux/kernel.h>
  25. #include <linux/pci.h>
  26. #include <linux/errno.h>
  27. #include <linux/acpi.h>
  28. #include <linux/hash.h>
  29. #include <linux/cpufreq.h>
  30. #include <linux/log2.h>
  31. #include <linux/dmi.h>
  32. #include <linux/atomic.h>
  33. #include <linux/crc16.h>
  34. #include "kfd_priv.h"
  35. #include "kfd_crat.h"
  36. #include "kfd_topology.h"
  37. #include "kfd_device_queue_manager.h"
  38. #include "kfd_svm.h"
  39. #include "kfd_debug.h"
  40. #include "amdgpu_amdkfd.h"
  41. #include "amdgpu_ras.h"
  42. #include "amdgpu.h"
  43. /* topology_device_list - Master list of all topology devices */
  44. static struct list_head topology_device_list;
  45. static struct kfd_system_properties sys_props;
  46. static DECLARE_RWSEM(topology_lock);
  47. static uint32_t topology_crat_proximity_domain;
  48. struct kfd_topology_device *kfd_topology_device_by_proximity_domain_no_lock(
  49. uint32_t proximity_domain)
  50. {
  51. struct kfd_topology_device *top_dev;
  52. struct kfd_topology_device *device = NULL;
  53. list_for_each_entry(top_dev, &topology_device_list, list)
  54. if (top_dev->proximity_domain == proximity_domain) {
  55. device = top_dev;
  56. break;
  57. }
  58. return device;
  59. }
  60. struct kfd_topology_device *kfd_topology_device_by_proximity_domain(
  61. uint32_t proximity_domain)
  62. {
  63. struct kfd_topology_device *device = NULL;
  64. down_read(&topology_lock);
  65. device = kfd_topology_device_by_proximity_domain_no_lock(
  66. proximity_domain);
  67. up_read(&topology_lock);
  68. return device;
  69. }
  70. struct kfd_topology_device *kfd_topology_device_by_id(uint32_t gpu_id)
  71. {
  72. struct kfd_topology_device *top_dev = NULL;
  73. struct kfd_topology_device *ret = NULL;
  74. down_read(&topology_lock);
  75. list_for_each_entry(top_dev, &topology_device_list, list)
  76. if (top_dev->gpu_id == gpu_id) {
  77. ret = top_dev;
  78. break;
  79. }
  80. up_read(&topology_lock);
  81. return ret;
  82. }
  83. struct kfd_node *kfd_device_by_id(uint32_t gpu_id)
  84. {
  85. struct kfd_topology_device *top_dev;
  86. top_dev = kfd_topology_device_by_id(gpu_id);
  87. if (!top_dev)
  88. return NULL;
  89. return top_dev->gpu;
  90. }
  91. struct kfd_node *kfd_device_by_pci_dev(const struct pci_dev *pdev)
  92. {
  93. struct kfd_topology_device *top_dev;
  94. struct kfd_node *device = NULL;
  95. down_read(&topology_lock);
  96. list_for_each_entry(top_dev, &topology_device_list, list)
  97. if (top_dev->gpu && top_dev->gpu->adev->pdev == pdev) {
  98. device = top_dev->gpu;
  99. break;
  100. }
  101. up_read(&topology_lock);
  102. return device;
  103. }
  104. /* Called with write topology_lock acquired */
  105. static void kfd_release_topology_device(struct kfd_topology_device *dev)
  106. {
  107. struct kfd_mem_properties *mem;
  108. struct kfd_cache_properties *cache;
  109. struct kfd_iolink_properties *iolink;
  110. struct kfd_iolink_properties *p2plink;
  111. struct kfd_perf_properties *perf;
  112. list_del(&dev->list);
  113. while (dev->mem_props.next != &dev->mem_props) {
  114. mem = container_of(dev->mem_props.next,
  115. struct kfd_mem_properties, list);
  116. list_del(&mem->list);
  117. kfree(mem);
  118. }
  119. while (dev->cache_props.next != &dev->cache_props) {
  120. cache = container_of(dev->cache_props.next,
  121. struct kfd_cache_properties, list);
  122. list_del(&cache->list);
  123. kfree(cache);
  124. }
  125. while (dev->io_link_props.next != &dev->io_link_props) {
  126. iolink = container_of(dev->io_link_props.next,
  127. struct kfd_iolink_properties, list);
  128. list_del(&iolink->list);
  129. kfree(iolink);
  130. }
  131. while (dev->p2p_link_props.next != &dev->p2p_link_props) {
  132. p2plink = container_of(dev->p2p_link_props.next,
  133. struct kfd_iolink_properties, list);
  134. list_del(&p2plink->list);
  135. kfree(p2plink);
  136. }
  137. while (dev->perf_props.next != &dev->perf_props) {
  138. perf = container_of(dev->perf_props.next,
  139. struct kfd_perf_properties, list);
  140. list_del(&perf->list);
  141. kfree(perf);
  142. }
  143. kfree(dev);
  144. }
  145. void kfd_release_topology_device_list(struct list_head *device_list)
  146. {
  147. struct kfd_topology_device *dev;
  148. while (!list_empty(device_list)) {
  149. dev = list_first_entry(device_list,
  150. struct kfd_topology_device, list);
  151. kfd_release_topology_device(dev);
  152. }
  153. }
  154. static void kfd_release_live_view(void)
  155. {
  156. kfd_release_topology_device_list(&topology_device_list);
  157. memset(&sys_props, 0, sizeof(sys_props));
  158. }
  159. struct kfd_topology_device *kfd_create_topology_device(
  160. struct list_head *device_list)
  161. {
  162. struct kfd_topology_device *dev;
  163. dev = kfd_alloc_struct(dev);
  164. if (!dev) {
  165. pr_err("No memory to allocate a topology device");
  166. return NULL;
  167. }
  168. INIT_LIST_HEAD(&dev->mem_props);
  169. INIT_LIST_HEAD(&dev->cache_props);
  170. INIT_LIST_HEAD(&dev->io_link_props);
  171. INIT_LIST_HEAD(&dev->p2p_link_props);
  172. INIT_LIST_HEAD(&dev->perf_props);
  173. list_add_tail(&dev->list, device_list);
  174. return dev;
  175. }
  176. #define sysfs_show_gen_prop(buffer, offs, fmt, ...) \
  177. (offs += snprintf(buffer+offs, PAGE_SIZE-offs, \
  178. fmt, __VA_ARGS__))
  179. #define sysfs_show_32bit_prop(buffer, offs, name, value) \
  180. sysfs_show_gen_prop(buffer, offs, "%s %u\n", name, value)
  181. #define sysfs_show_64bit_prop(buffer, offs, name, value) \
  182. sysfs_show_gen_prop(buffer, offs, "%s %llu\n", name, value)
  183. #define sysfs_show_32bit_val(buffer, offs, value) \
  184. sysfs_show_gen_prop(buffer, offs, "%u\n", value)
  185. #define sysfs_show_str_val(buffer, offs, value) \
  186. sysfs_show_gen_prop(buffer, offs, "%s\n", value)
  187. static ssize_t sysprops_show(struct kobject *kobj, struct attribute *attr,
  188. char *buffer)
  189. {
  190. int offs = 0;
  191. /* Making sure that the buffer is an empty string */
  192. buffer[0] = 0;
  193. if (attr == &sys_props.attr_genid) {
  194. sysfs_show_32bit_val(buffer, offs,
  195. sys_props.generation_count);
  196. } else if (attr == &sys_props.attr_props) {
  197. sysfs_show_64bit_prop(buffer, offs, "platform_oem",
  198. sys_props.platform_oem);
  199. sysfs_show_64bit_prop(buffer, offs, "platform_id",
  200. sys_props.platform_id);
  201. sysfs_show_64bit_prop(buffer, offs, "platform_rev",
  202. sys_props.platform_rev);
  203. } else {
  204. offs = -EINVAL;
  205. }
  206. return offs;
  207. }
  208. static void kfd_topology_kobj_release(struct kobject *kobj)
  209. {
  210. kfree(kobj);
  211. }
  212. static const struct sysfs_ops sysprops_ops = {
  213. .show = sysprops_show,
  214. };
  215. static const struct kobj_type sysprops_type = {
  216. .release = kfd_topology_kobj_release,
  217. .sysfs_ops = &sysprops_ops,
  218. };
  219. static ssize_t iolink_show(struct kobject *kobj, struct attribute *attr,
  220. char *buffer)
  221. {
  222. int offs = 0;
  223. struct kfd_iolink_properties *iolink;
  224. /* Making sure that the buffer is an empty string */
  225. buffer[0] = 0;
  226. iolink = container_of(attr, struct kfd_iolink_properties, attr);
  227. if (iolink->gpu && kfd_devcgroup_check_permission(iolink->gpu))
  228. return -EPERM;
  229. sysfs_show_32bit_prop(buffer, offs, "type", iolink->iolink_type);
  230. sysfs_show_32bit_prop(buffer, offs, "version_major", iolink->ver_maj);
  231. sysfs_show_32bit_prop(buffer, offs, "version_minor", iolink->ver_min);
  232. sysfs_show_32bit_prop(buffer, offs, "node_from", iolink->node_from);
  233. sysfs_show_32bit_prop(buffer, offs, "node_to", iolink->node_to);
  234. sysfs_show_32bit_prop(buffer, offs, "weight", iolink->weight);
  235. sysfs_show_32bit_prop(buffer, offs, "min_latency", iolink->min_latency);
  236. sysfs_show_32bit_prop(buffer, offs, "max_latency", iolink->max_latency);
  237. sysfs_show_32bit_prop(buffer, offs, "min_bandwidth",
  238. iolink->min_bandwidth);
  239. sysfs_show_32bit_prop(buffer, offs, "max_bandwidth",
  240. iolink->max_bandwidth);
  241. sysfs_show_32bit_prop(buffer, offs, "recommended_transfer_size",
  242. iolink->rec_transfer_size);
  243. sysfs_show_32bit_prop(buffer, offs, "recommended_sdma_engine_id_mask",
  244. iolink->rec_sdma_eng_id_mask);
  245. sysfs_show_32bit_prop(buffer, offs, "flags", iolink->flags);
  246. return offs;
  247. }
  248. static const struct sysfs_ops iolink_ops = {
  249. .show = iolink_show,
  250. };
  251. static const struct kobj_type iolink_type = {
  252. .release = kfd_topology_kobj_release,
  253. .sysfs_ops = &iolink_ops,
  254. };
  255. static ssize_t mem_show(struct kobject *kobj, struct attribute *attr,
  256. char *buffer)
  257. {
  258. int offs = 0;
  259. struct kfd_mem_properties *mem;
  260. /* Making sure that the buffer is an empty string */
  261. buffer[0] = 0;
  262. mem = container_of(attr, struct kfd_mem_properties, attr);
  263. if (mem->gpu && kfd_devcgroup_check_permission(mem->gpu))
  264. return -EPERM;
  265. sysfs_show_32bit_prop(buffer, offs, "heap_type", mem->heap_type);
  266. sysfs_show_64bit_prop(buffer, offs, "size_in_bytes",
  267. mem->size_in_bytes);
  268. sysfs_show_32bit_prop(buffer, offs, "flags", mem->flags);
  269. sysfs_show_32bit_prop(buffer, offs, "width", mem->width);
  270. sysfs_show_32bit_prop(buffer, offs, "mem_clk_max",
  271. mem->mem_clk_max);
  272. return offs;
  273. }
  274. static const struct sysfs_ops mem_ops = {
  275. .show = mem_show,
  276. };
  277. static const struct kobj_type mem_type = {
  278. .release = kfd_topology_kobj_release,
  279. .sysfs_ops = &mem_ops,
  280. };
  281. static ssize_t kfd_cache_show(struct kobject *kobj, struct attribute *attr,
  282. char *buffer)
  283. {
  284. int offs = 0;
  285. uint32_t i, j;
  286. struct kfd_cache_properties *cache;
  287. /* Making sure that the buffer is an empty string */
  288. buffer[0] = 0;
  289. cache = container_of(attr, struct kfd_cache_properties, attr);
  290. if (cache->gpu && kfd_devcgroup_check_permission(cache->gpu))
  291. return -EPERM;
  292. sysfs_show_32bit_prop(buffer, offs, "processor_id_low",
  293. cache->processor_id_low);
  294. sysfs_show_32bit_prop(buffer, offs, "level", cache->cache_level);
  295. sysfs_show_32bit_prop(buffer, offs, "size", cache->cache_size);
  296. sysfs_show_32bit_prop(buffer, offs, "cache_line_size",
  297. cache->cacheline_size);
  298. sysfs_show_32bit_prop(buffer, offs, "cache_lines_per_tag",
  299. cache->cachelines_per_tag);
  300. sysfs_show_32bit_prop(buffer, offs, "association", cache->cache_assoc);
  301. sysfs_show_32bit_prop(buffer, offs, "latency", cache->cache_latency);
  302. sysfs_show_32bit_prop(buffer, offs, "type", cache->cache_type);
  303. offs += snprintf(buffer+offs, PAGE_SIZE-offs, "sibling_map ");
  304. for (i = 0; i < cache->sibling_map_size; i++)
  305. for (j = 0; j < sizeof(cache->sibling_map[0])*8; j++)
  306. /* Check each bit */
  307. offs += snprintf(buffer+offs, PAGE_SIZE-offs, "%d,",
  308. (cache->sibling_map[i] >> j) & 1);
  309. /* Replace the last "," with end of line */
  310. buffer[offs-1] = '\n';
  311. return offs;
  312. }
  313. static const struct sysfs_ops cache_ops = {
  314. .show = kfd_cache_show,
  315. };
  316. static const struct kobj_type cache_type = {
  317. .release = kfd_topology_kobj_release,
  318. .sysfs_ops = &cache_ops,
  319. };
  320. /****** Sysfs of Performance Counters ******/
  321. struct kfd_perf_attr {
  322. struct kobj_attribute attr;
  323. uint32_t data;
  324. };
  325. static ssize_t perf_show(struct kobject *kobj, struct kobj_attribute *attrs,
  326. char *buf)
  327. {
  328. int offs = 0;
  329. struct kfd_perf_attr *attr;
  330. buf[0] = 0;
  331. attr = container_of(attrs, struct kfd_perf_attr, attr);
  332. if (!attr->data) /* invalid data for PMC */
  333. return 0;
  334. else
  335. return sysfs_show_32bit_val(buf, offs, attr->data);
  336. }
  337. #define KFD_PERF_DESC(_name, _data) \
  338. { \
  339. .attr = __ATTR(_name, 0444, perf_show, NULL), \
  340. .data = _data, \
  341. }
  342. static struct kfd_perf_attr perf_attr_iommu[] = {
  343. KFD_PERF_DESC(max_concurrent, 0),
  344. KFD_PERF_DESC(num_counters, 0),
  345. KFD_PERF_DESC(counter_ids, 0),
  346. };
  347. /****************************************/
  348. static ssize_t node_show(struct kobject *kobj, struct attribute *attr,
  349. char *buffer)
  350. {
  351. int offs = 0;
  352. struct kfd_topology_device *dev;
  353. uint32_t log_max_watch_addr;
  354. /* Making sure that the buffer is an empty string */
  355. buffer[0] = 0;
  356. if (strcmp(attr->name, "gpu_id") == 0) {
  357. dev = container_of(attr, struct kfd_topology_device,
  358. attr_gpuid);
  359. if (dev->gpu && kfd_devcgroup_check_permission(dev->gpu))
  360. return -EPERM;
  361. return sysfs_show_32bit_val(buffer, offs, dev->gpu_id);
  362. }
  363. if (strcmp(attr->name, "name") == 0) {
  364. dev = container_of(attr, struct kfd_topology_device,
  365. attr_name);
  366. if (dev->gpu && kfd_devcgroup_check_permission(dev->gpu))
  367. return -EPERM;
  368. return sysfs_show_str_val(buffer, offs, dev->node_props.name);
  369. }
  370. dev = container_of(attr, struct kfd_topology_device,
  371. attr_props);
  372. if (dev->gpu && kfd_devcgroup_check_permission(dev->gpu))
  373. return -EPERM;
  374. sysfs_show_32bit_prop(buffer, offs, "cpu_cores_count",
  375. dev->node_props.cpu_cores_count);
  376. sysfs_show_32bit_prop(buffer, offs, "simd_count",
  377. dev->gpu ? dev->node_props.simd_count : 0);
  378. sysfs_show_32bit_prop(buffer, offs, "mem_banks_count",
  379. dev->node_props.mem_banks_count);
  380. sysfs_show_32bit_prop(buffer, offs, "caches_count",
  381. dev->node_props.caches_count);
  382. sysfs_show_32bit_prop(buffer, offs, "io_links_count",
  383. dev->node_props.io_links_count);
  384. sysfs_show_32bit_prop(buffer, offs, "p2p_links_count",
  385. dev->node_props.p2p_links_count);
  386. sysfs_show_32bit_prop(buffer, offs, "cpu_core_id_base",
  387. dev->node_props.cpu_core_id_base);
  388. sysfs_show_32bit_prop(buffer, offs, "simd_id_base",
  389. dev->node_props.simd_id_base);
  390. sysfs_show_32bit_prop(buffer, offs, "max_waves_per_simd",
  391. dev->node_props.max_waves_per_simd);
  392. sysfs_show_32bit_prop(buffer, offs, "lds_size_in_kb",
  393. dev->node_props.lds_size_in_kb);
  394. sysfs_show_32bit_prop(buffer, offs, "gds_size_in_kb",
  395. dev->node_props.gds_size_in_kb);
  396. sysfs_show_32bit_prop(buffer, offs, "num_gws",
  397. dev->node_props.num_gws);
  398. sysfs_show_32bit_prop(buffer, offs, "wave_front_size",
  399. dev->node_props.wave_front_size);
  400. sysfs_show_32bit_prop(buffer, offs, "array_count",
  401. dev->gpu ? (dev->node_props.array_count *
  402. NUM_XCC(dev->gpu->xcc_mask)) : 0);
  403. sysfs_show_32bit_prop(buffer, offs, "simd_arrays_per_engine",
  404. dev->node_props.simd_arrays_per_engine);
  405. sysfs_show_32bit_prop(buffer, offs, "cu_per_simd_array",
  406. dev->node_props.cu_per_simd_array);
  407. sysfs_show_32bit_prop(buffer, offs, "simd_per_cu",
  408. dev->node_props.simd_per_cu);
  409. sysfs_show_32bit_prop(buffer, offs, "max_slots_scratch_cu",
  410. dev->node_props.max_slots_scratch_cu);
  411. sysfs_show_32bit_prop(buffer, offs, "gfx_target_version",
  412. dev->node_props.gfx_target_version);
  413. sysfs_show_32bit_prop(buffer, offs, "vendor_id",
  414. dev->node_props.vendor_id);
  415. sysfs_show_32bit_prop(buffer, offs, "device_id",
  416. dev->node_props.device_id);
  417. sysfs_show_32bit_prop(buffer, offs, "location_id",
  418. dev->node_props.location_id);
  419. sysfs_show_32bit_prop(buffer, offs, "domain",
  420. dev->node_props.domain);
  421. sysfs_show_32bit_prop(buffer, offs, "drm_render_minor",
  422. dev->node_props.drm_render_minor);
  423. sysfs_show_64bit_prop(buffer, offs, "hive_id",
  424. dev->node_props.hive_id);
  425. sysfs_show_32bit_prop(buffer, offs, "num_sdma_engines",
  426. dev->node_props.num_sdma_engines);
  427. sysfs_show_32bit_prop(buffer, offs, "num_sdma_xgmi_engines",
  428. dev->node_props.num_sdma_xgmi_engines);
  429. sysfs_show_32bit_prop(buffer, offs, "num_sdma_queues_per_engine",
  430. dev->node_props.num_sdma_queues_per_engine);
  431. sysfs_show_32bit_prop(buffer, offs, "num_cp_queues",
  432. dev->node_props.num_cp_queues);
  433. if (dev->gpu) {
  434. log_max_watch_addr =
  435. __ilog2_u32(dev->gpu->kfd->device_info.num_of_watch_points);
  436. if (log_max_watch_addr) {
  437. dev->node_props.capability |=
  438. HSA_CAP_WATCH_POINTS_SUPPORTED;
  439. dev->node_props.capability |=
  440. ((log_max_watch_addr <<
  441. HSA_CAP_WATCH_POINTS_TOTALBITS_SHIFT) &
  442. HSA_CAP_WATCH_POINTS_TOTALBITS_MASK);
  443. }
  444. if (dev->gpu->adev->asic_type == CHIP_TONGA)
  445. dev->node_props.capability |=
  446. HSA_CAP_AQL_QUEUE_DOUBLE_MAP;
  447. sysfs_show_32bit_prop(buffer, offs, "max_engine_clk_fcompute",
  448. dev->node_props.max_engine_clk_fcompute);
  449. sysfs_show_64bit_prop(buffer, offs, "local_mem_size", 0ULL);
  450. sysfs_show_32bit_prop(buffer, offs, "fw_version",
  451. dev->gpu->kfd->mec_fw_version);
  452. sysfs_show_32bit_prop(buffer, offs, "capability",
  453. dev->node_props.capability);
  454. sysfs_show_64bit_prop(buffer, offs, "debug_prop",
  455. dev->node_props.debug_prop);
  456. sysfs_show_32bit_prop(buffer, offs, "sdma_fw_version",
  457. dev->gpu->kfd->sdma_fw_version);
  458. sysfs_show_64bit_prop(buffer, offs, "unique_id",
  459. dev->gpu->adev->unique_id);
  460. sysfs_show_32bit_prop(buffer, offs, "num_xcc",
  461. NUM_XCC(dev->gpu->xcc_mask));
  462. }
  463. return sysfs_show_32bit_prop(buffer, offs, "max_engine_clk_ccompute",
  464. cpufreq_quick_get_max(0)/1000);
  465. }
  466. static const struct sysfs_ops node_ops = {
  467. .show = node_show,
  468. };
  469. static const struct kobj_type node_type = {
  470. .release = kfd_topology_kobj_release,
  471. .sysfs_ops = &node_ops,
  472. };
  473. static void kfd_remove_sysfs_file(struct kobject *kobj, struct attribute *attr)
  474. {
  475. sysfs_remove_file(kobj, attr);
  476. kobject_del(kobj);
  477. kobject_put(kobj);
  478. }
  479. static void kfd_remove_sysfs_node_entry(struct kfd_topology_device *dev)
  480. {
  481. struct kfd_iolink_properties *p2plink;
  482. struct kfd_iolink_properties *iolink;
  483. struct kfd_cache_properties *cache;
  484. struct kfd_mem_properties *mem;
  485. struct kfd_perf_properties *perf;
  486. if (dev->kobj_iolink) {
  487. list_for_each_entry(iolink, &dev->io_link_props, list)
  488. if (iolink->kobj) {
  489. kfd_remove_sysfs_file(iolink->kobj,
  490. &iolink->attr);
  491. iolink->kobj = NULL;
  492. }
  493. kobject_del(dev->kobj_iolink);
  494. kobject_put(dev->kobj_iolink);
  495. dev->kobj_iolink = NULL;
  496. }
  497. if (dev->kobj_p2plink) {
  498. list_for_each_entry(p2plink, &dev->p2p_link_props, list)
  499. if (p2plink->kobj) {
  500. kfd_remove_sysfs_file(p2plink->kobj,
  501. &p2plink->attr);
  502. p2plink->kobj = NULL;
  503. }
  504. kobject_del(dev->kobj_p2plink);
  505. kobject_put(dev->kobj_p2plink);
  506. dev->kobj_p2plink = NULL;
  507. }
  508. if (dev->kobj_cache) {
  509. list_for_each_entry(cache, &dev->cache_props, list)
  510. if (cache->kobj) {
  511. kfd_remove_sysfs_file(cache->kobj,
  512. &cache->attr);
  513. cache->kobj = NULL;
  514. }
  515. kobject_del(dev->kobj_cache);
  516. kobject_put(dev->kobj_cache);
  517. dev->kobj_cache = NULL;
  518. }
  519. if (dev->kobj_mem) {
  520. list_for_each_entry(mem, &dev->mem_props, list)
  521. if (mem->kobj) {
  522. kfd_remove_sysfs_file(mem->kobj, &mem->attr);
  523. mem->kobj = NULL;
  524. }
  525. kobject_del(dev->kobj_mem);
  526. kobject_put(dev->kobj_mem);
  527. dev->kobj_mem = NULL;
  528. }
  529. if (dev->kobj_perf) {
  530. list_for_each_entry(perf, &dev->perf_props, list) {
  531. kfree(perf->attr_group);
  532. perf->attr_group = NULL;
  533. }
  534. kobject_del(dev->kobj_perf);
  535. kobject_put(dev->kobj_perf);
  536. dev->kobj_perf = NULL;
  537. }
  538. if (dev->kobj_node) {
  539. sysfs_remove_file(dev->kobj_node, &dev->attr_gpuid);
  540. sysfs_remove_file(dev->kobj_node, &dev->attr_name);
  541. sysfs_remove_file(dev->kobj_node, &dev->attr_props);
  542. kobject_del(dev->kobj_node);
  543. kobject_put(dev->kobj_node);
  544. dev->kobj_node = NULL;
  545. }
  546. }
  547. static int kfd_build_sysfs_node_entry(struct kfd_topology_device *dev,
  548. uint32_t id)
  549. {
  550. struct kfd_iolink_properties *p2plink;
  551. struct kfd_iolink_properties *iolink;
  552. struct kfd_cache_properties *cache;
  553. struct kfd_mem_properties *mem;
  554. struct kfd_perf_properties *perf;
  555. int ret;
  556. uint32_t i, num_attrs;
  557. struct attribute **attrs;
  558. if (WARN_ON(dev->kobj_node))
  559. return -EEXIST;
  560. /*
  561. * Creating the sysfs folders
  562. */
  563. dev->kobj_node = kfd_alloc_struct(dev->kobj_node);
  564. if (!dev->kobj_node)
  565. return -ENOMEM;
  566. ret = kobject_init_and_add(dev->kobj_node, &node_type,
  567. sys_props.kobj_nodes, "%d", id);
  568. if (ret < 0) {
  569. kobject_put(dev->kobj_node);
  570. return ret;
  571. }
  572. dev->kobj_mem = kobject_create_and_add("mem_banks", dev->kobj_node);
  573. if (!dev->kobj_mem)
  574. return -ENOMEM;
  575. dev->kobj_cache = kobject_create_and_add("caches", dev->kobj_node);
  576. if (!dev->kobj_cache)
  577. return -ENOMEM;
  578. dev->kobj_iolink = kobject_create_and_add("io_links", dev->kobj_node);
  579. if (!dev->kobj_iolink)
  580. return -ENOMEM;
  581. dev->kobj_p2plink = kobject_create_and_add("p2p_links", dev->kobj_node);
  582. if (!dev->kobj_p2plink)
  583. return -ENOMEM;
  584. dev->kobj_perf = kobject_create_and_add("perf", dev->kobj_node);
  585. if (!dev->kobj_perf)
  586. return -ENOMEM;
  587. /*
  588. * Creating sysfs files for node properties
  589. */
  590. dev->attr_gpuid.name = "gpu_id";
  591. dev->attr_gpuid.mode = KFD_SYSFS_FILE_MODE;
  592. sysfs_attr_init(&dev->attr_gpuid);
  593. dev->attr_name.name = "name";
  594. dev->attr_name.mode = KFD_SYSFS_FILE_MODE;
  595. sysfs_attr_init(&dev->attr_name);
  596. dev->attr_props.name = "properties";
  597. dev->attr_props.mode = KFD_SYSFS_FILE_MODE;
  598. sysfs_attr_init(&dev->attr_props);
  599. ret = sysfs_create_file(dev->kobj_node, &dev->attr_gpuid);
  600. if (ret < 0)
  601. return ret;
  602. ret = sysfs_create_file(dev->kobj_node, &dev->attr_name);
  603. if (ret < 0)
  604. return ret;
  605. ret = sysfs_create_file(dev->kobj_node, &dev->attr_props);
  606. if (ret < 0)
  607. return ret;
  608. i = 0;
  609. list_for_each_entry(mem, &dev->mem_props, list) {
  610. mem->kobj = kzalloc(sizeof(struct kobject), GFP_KERNEL);
  611. if (!mem->kobj)
  612. return -ENOMEM;
  613. ret = kobject_init_and_add(mem->kobj, &mem_type,
  614. dev->kobj_mem, "%d", i);
  615. if (ret < 0) {
  616. kobject_put(mem->kobj);
  617. return ret;
  618. }
  619. mem->attr.name = "properties";
  620. mem->attr.mode = KFD_SYSFS_FILE_MODE;
  621. sysfs_attr_init(&mem->attr);
  622. ret = sysfs_create_file(mem->kobj, &mem->attr);
  623. if (ret < 0)
  624. return ret;
  625. i++;
  626. }
  627. i = 0;
  628. list_for_each_entry(cache, &dev->cache_props, list) {
  629. cache->kobj = kzalloc(sizeof(struct kobject), GFP_KERNEL);
  630. if (!cache->kobj)
  631. return -ENOMEM;
  632. ret = kobject_init_and_add(cache->kobj, &cache_type,
  633. dev->kobj_cache, "%d", i);
  634. if (ret < 0) {
  635. kobject_put(cache->kobj);
  636. return ret;
  637. }
  638. cache->attr.name = "properties";
  639. cache->attr.mode = KFD_SYSFS_FILE_MODE;
  640. sysfs_attr_init(&cache->attr);
  641. ret = sysfs_create_file(cache->kobj, &cache->attr);
  642. if (ret < 0)
  643. return ret;
  644. i++;
  645. }
  646. i = 0;
  647. list_for_each_entry(iolink, &dev->io_link_props, list) {
  648. iolink->kobj = kzalloc(sizeof(struct kobject), GFP_KERNEL);
  649. if (!iolink->kobj)
  650. return -ENOMEM;
  651. ret = kobject_init_and_add(iolink->kobj, &iolink_type,
  652. dev->kobj_iolink, "%d", i);
  653. if (ret < 0) {
  654. kobject_put(iolink->kobj);
  655. return ret;
  656. }
  657. iolink->attr.name = "properties";
  658. iolink->attr.mode = KFD_SYSFS_FILE_MODE;
  659. sysfs_attr_init(&iolink->attr);
  660. ret = sysfs_create_file(iolink->kobj, &iolink->attr);
  661. if (ret < 0)
  662. return ret;
  663. i++;
  664. }
  665. i = 0;
  666. list_for_each_entry(p2plink, &dev->p2p_link_props, list) {
  667. p2plink->kobj = kzalloc(sizeof(struct kobject), GFP_KERNEL);
  668. if (!p2plink->kobj)
  669. return -ENOMEM;
  670. ret = kobject_init_and_add(p2plink->kobj, &iolink_type,
  671. dev->kobj_p2plink, "%d", i);
  672. if (ret < 0) {
  673. kobject_put(p2plink->kobj);
  674. return ret;
  675. }
  676. p2plink->attr.name = "properties";
  677. p2plink->attr.mode = KFD_SYSFS_FILE_MODE;
  678. sysfs_attr_init(&p2plink->attr);
  679. ret = sysfs_create_file(p2plink->kobj, &p2plink->attr);
  680. if (ret < 0)
  681. return ret;
  682. i++;
  683. }
  684. /* All hardware blocks have the same number of attributes. */
  685. num_attrs = ARRAY_SIZE(perf_attr_iommu);
  686. list_for_each_entry(perf, &dev->perf_props, list) {
  687. perf->attr_group = kzalloc(sizeof(struct kfd_perf_attr)
  688. * num_attrs + sizeof(struct attribute_group),
  689. GFP_KERNEL);
  690. if (!perf->attr_group)
  691. return -ENOMEM;
  692. attrs = (struct attribute **)(perf->attr_group + 1);
  693. if (!strcmp(perf->block_name, "iommu")) {
  694. /* Information of IOMMU's num_counters and counter_ids is shown
  695. * under /sys/bus/event_source/devices/amd_iommu. We don't
  696. * duplicate here.
  697. */
  698. perf_attr_iommu[0].data = perf->max_concurrent;
  699. for (i = 0; i < num_attrs; i++)
  700. attrs[i] = &perf_attr_iommu[i].attr.attr;
  701. }
  702. perf->attr_group->name = perf->block_name;
  703. perf->attr_group->attrs = attrs;
  704. ret = sysfs_create_group(dev->kobj_perf, perf->attr_group);
  705. if (ret < 0)
  706. return ret;
  707. }
  708. return 0;
  709. }
  710. /* Called with write topology lock acquired */
  711. static int kfd_build_sysfs_node_tree(void)
  712. {
  713. struct kfd_topology_device *dev;
  714. int ret;
  715. uint32_t i = 0;
  716. list_for_each_entry(dev, &topology_device_list, list) {
  717. ret = kfd_build_sysfs_node_entry(dev, i);
  718. if (ret < 0)
  719. return ret;
  720. i++;
  721. }
  722. return 0;
  723. }
  724. /* Called with write topology lock acquired */
  725. static void kfd_remove_sysfs_node_tree(void)
  726. {
  727. struct kfd_topology_device *dev;
  728. list_for_each_entry(dev, &topology_device_list, list)
  729. kfd_remove_sysfs_node_entry(dev);
  730. }
  731. static int kfd_topology_update_sysfs(void)
  732. {
  733. int ret;
  734. if (!sys_props.kobj_topology) {
  735. sys_props.kobj_topology =
  736. kfd_alloc_struct(sys_props.kobj_topology);
  737. if (!sys_props.kobj_topology)
  738. return -ENOMEM;
  739. ret = kobject_init_and_add(sys_props.kobj_topology,
  740. &sysprops_type, &kfd_device->kobj,
  741. "topology");
  742. if (ret < 0) {
  743. kobject_put(sys_props.kobj_topology);
  744. return ret;
  745. }
  746. sys_props.kobj_nodes = kobject_create_and_add("nodes",
  747. sys_props.kobj_topology);
  748. if (!sys_props.kobj_nodes)
  749. return -ENOMEM;
  750. sys_props.attr_genid.name = "generation_id";
  751. sys_props.attr_genid.mode = KFD_SYSFS_FILE_MODE;
  752. sysfs_attr_init(&sys_props.attr_genid);
  753. ret = sysfs_create_file(sys_props.kobj_topology,
  754. &sys_props.attr_genid);
  755. if (ret < 0)
  756. return ret;
  757. sys_props.attr_props.name = "system_properties";
  758. sys_props.attr_props.mode = KFD_SYSFS_FILE_MODE;
  759. sysfs_attr_init(&sys_props.attr_props);
  760. ret = sysfs_create_file(sys_props.kobj_topology,
  761. &sys_props.attr_props);
  762. if (ret < 0)
  763. return ret;
  764. }
  765. kfd_remove_sysfs_node_tree();
  766. return kfd_build_sysfs_node_tree();
  767. }
  768. static void kfd_topology_release_sysfs(void)
  769. {
  770. kfd_remove_sysfs_node_tree();
  771. if (sys_props.kobj_topology) {
  772. sysfs_remove_file(sys_props.kobj_topology,
  773. &sys_props.attr_genid);
  774. sysfs_remove_file(sys_props.kobj_topology,
  775. &sys_props.attr_props);
  776. if (sys_props.kobj_nodes) {
  777. kobject_del(sys_props.kobj_nodes);
  778. kobject_put(sys_props.kobj_nodes);
  779. sys_props.kobj_nodes = NULL;
  780. }
  781. kobject_del(sys_props.kobj_topology);
  782. kobject_put(sys_props.kobj_topology);
  783. sys_props.kobj_topology = NULL;
  784. }
  785. }
  786. /* Called with write topology_lock acquired */
  787. static void kfd_topology_update_device_list(struct list_head *temp_list,
  788. struct list_head *master_list)
  789. {
  790. while (!list_empty(temp_list)) {
  791. list_move_tail(temp_list->next, master_list);
  792. sys_props.num_devices++;
  793. }
  794. }
  795. static void kfd_debug_print_topology(void)
  796. {
  797. struct kfd_topology_device *dev;
  798. down_read(&topology_lock);
  799. dev = list_last_entry(&topology_device_list,
  800. struct kfd_topology_device, list);
  801. if (dev) {
  802. if (dev->node_props.cpu_cores_count &&
  803. dev->node_props.simd_count) {
  804. pr_info("Topology: Add APU node [0x%0x:0x%0x]\n",
  805. dev->node_props.device_id,
  806. dev->node_props.vendor_id);
  807. } else if (dev->node_props.cpu_cores_count)
  808. pr_info("Topology: Add CPU node\n");
  809. else if (dev->node_props.simd_count)
  810. pr_info("Topology: Add dGPU node [0x%0x:0x%0x]\n",
  811. dev->node_props.device_id,
  812. dev->node_props.vendor_id);
  813. }
  814. up_read(&topology_lock);
  815. }
  816. /* Helper function for intializing platform_xx members of
  817. * kfd_system_properties. Uses OEM info from the last CPU/APU node.
  818. */
  819. static void kfd_update_system_properties(void)
  820. {
  821. struct kfd_topology_device *dev;
  822. down_read(&topology_lock);
  823. dev = list_last_entry(&topology_device_list,
  824. struct kfd_topology_device, list);
  825. if (dev) {
  826. sys_props.platform_id = dev->oem_id64;
  827. sys_props.platform_oem = *((uint64_t *)dev->oem_table_id);
  828. sys_props.platform_rev = dev->oem_revision;
  829. }
  830. up_read(&topology_lock);
  831. }
  832. static void find_system_memory(const struct dmi_header *dm,
  833. void *private)
  834. {
  835. struct kfd_mem_properties *mem;
  836. u16 mem_width, mem_clock;
  837. struct kfd_topology_device *kdev =
  838. (struct kfd_topology_device *)private;
  839. const u8 *dmi_data = (const u8 *)(dm + 1);
  840. if (dm->type == DMI_ENTRY_MEM_DEVICE && dm->length >= 0x15) {
  841. mem_width = (u16)(*(const u16 *)(dmi_data + 0x6));
  842. mem_clock = (u16)(*(const u16 *)(dmi_data + 0x11));
  843. list_for_each_entry(mem, &kdev->mem_props, list) {
  844. if (mem_width != 0xFFFF && mem_width != 0)
  845. mem->width = mem_width;
  846. if (mem_clock != 0)
  847. mem->mem_clk_max = mem_clock;
  848. }
  849. }
  850. }
  851. /* kfd_add_non_crat_information - Add information that is not currently
  852. * defined in CRAT but is necessary for KFD topology
  853. * @dev - topology device to which addition info is added
  854. */
  855. static void kfd_add_non_crat_information(struct kfd_topology_device *kdev)
  856. {
  857. /* Check if CPU only node. */
  858. if (!kdev->gpu) {
  859. /* Add system memory information */
  860. dmi_walk(find_system_memory, kdev);
  861. }
  862. /* TODO: For GPU node, rearrange code from kfd_topology_add_device */
  863. }
  864. int kfd_topology_init(void)
  865. {
  866. void *crat_image = NULL;
  867. size_t image_size = 0;
  868. int ret;
  869. struct list_head temp_topology_device_list;
  870. int cpu_only_node = 0;
  871. struct kfd_topology_device *kdev;
  872. int proximity_domain;
  873. /* topology_device_list - Master list of all topology devices
  874. * temp_topology_device_list - temporary list created while parsing CRAT
  875. * or VCRAT. Once parsing is complete the contents of list is moved to
  876. * topology_device_list
  877. */
  878. /* Initialize the head for the both the lists */
  879. INIT_LIST_HEAD(&topology_device_list);
  880. INIT_LIST_HEAD(&temp_topology_device_list);
  881. init_rwsem(&topology_lock);
  882. memset(&sys_props, 0, sizeof(sys_props));
  883. /* Proximity domains in ACPI CRAT tables start counting at
  884. * 0. The same should be true for virtual CRAT tables created
  885. * at this stage. GPUs added later in kfd_topology_add_device
  886. * use a counter.
  887. */
  888. proximity_domain = 0;
  889. ret = kfd_create_crat_image_virtual(&crat_image, &image_size,
  890. COMPUTE_UNIT_CPU, NULL,
  891. proximity_domain);
  892. cpu_only_node = 1;
  893. if (ret) {
  894. pr_err("Error creating VCRAT table for CPU\n");
  895. return ret;
  896. }
  897. ret = kfd_parse_crat_table(crat_image,
  898. &temp_topology_device_list,
  899. proximity_domain);
  900. if (ret) {
  901. pr_err("Error parsing VCRAT table for CPU\n");
  902. goto err;
  903. }
  904. kdev = list_first_entry(&temp_topology_device_list,
  905. struct kfd_topology_device, list);
  906. down_write(&topology_lock);
  907. kfd_topology_update_device_list(&temp_topology_device_list,
  908. &topology_device_list);
  909. topology_crat_proximity_domain = sys_props.num_devices-1;
  910. ret = kfd_topology_update_sysfs();
  911. up_write(&topology_lock);
  912. if (!ret) {
  913. sys_props.generation_count++;
  914. kfd_update_system_properties();
  915. kfd_debug_print_topology();
  916. } else
  917. pr_err("Failed to update topology in sysfs ret=%d\n", ret);
  918. /* For nodes with GPU, this information gets added
  919. * when GPU is detected (kfd_topology_add_device).
  920. */
  921. if (cpu_only_node) {
  922. /* Add additional information to CPU only node created above */
  923. down_write(&topology_lock);
  924. kdev = list_first_entry(&topology_device_list,
  925. struct kfd_topology_device, list);
  926. up_write(&topology_lock);
  927. kfd_add_non_crat_information(kdev);
  928. }
  929. err:
  930. kfd_destroy_crat_image(crat_image);
  931. return ret;
  932. }
  933. void kfd_topology_shutdown(void)
  934. {
  935. down_write(&topology_lock);
  936. kfd_topology_release_sysfs();
  937. kfd_release_live_view();
  938. up_write(&topology_lock);
  939. }
  940. static uint32_t kfd_generate_gpu_id(struct kfd_node *gpu)
  941. {
  942. uint32_t gpu_id;
  943. uint32_t buf[8];
  944. uint64_t local_mem_size;
  945. struct kfd_topology_device *dev;
  946. bool is_unique;
  947. uint8_t *crc_buf;
  948. if (!gpu)
  949. return 0;
  950. crc_buf = (uint8_t *)&buf;
  951. local_mem_size = gpu->local_mem_info.local_mem_size_private +
  952. gpu->local_mem_info.local_mem_size_public;
  953. buf[0] = gpu->adev->pdev->devfn;
  954. buf[1] = gpu->adev->pdev->subsystem_vendor |
  955. (gpu->adev->pdev->subsystem_device << 16);
  956. buf[2] = pci_domain_nr(gpu->adev->pdev->bus);
  957. buf[3] = gpu->adev->pdev->device;
  958. buf[4] = gpu->adev->pdev->bus->number;
  959. buf[5] = lower_32_bits(local_mem_size);
  960. buf[6] = upper_32_bits(local_mem_size);
  961. buf[7] = (ffs(gpu->xcc_mask) - 1) | (NUM_XCC(gpu->xcc_mask) << 16);
  962. gpu_id = crc16(0, crc_buf, sizeof(buf)) &
  963. ((1 << KFD_GPU_ID_HASH_WIDTH) - 1);
  964. /* There is a very small possibility when generating a
  965. * 16 (KFD_GPU_ID_HASH_WIDTH) bit value from 8 word buffer
  966. * that the value could be 0 or non-unique. So, check if
  967. * it is unique and non-zero. If not unique increment till
  968. * unique one is found. In case of overflow, restart from 1
  969. */
  970. down_read(&topology_lock);
  971. do {
  972. is_unique = true;
  973. if (!gpu_id)
  974. gpu_id = 1;
  975. list_for_each_entry(dev, &topology_device_list, list) {
  976. if (dev->gpu && dev->gpu_id == gpu_id) {
  977. is_unique = false;
  978. break;
  979. }
  980. }
  981. if (unlikely(!is_unique))
  982. gpu_id = (gpu_id + 1) &
  983. ((1 << KFD_GPU_ID_HASH_WIDTH) - 1);
  984. } while (!is_unique);
  985. up_read(&topology_lock);
  986. return gpu_id;
  987. }
  988. /* kfd_assign_gpu - Attach @gpu to the correct kfd topology device. If
  989. * the GPU device is not already present in the topology device
  990. * list then return NULL. This means a new topology device has to
  991. * be created for this GPU.
  992. */
  993. static struct kfd_topology_device *kfd_assign_gpu(struct kfd_node *gpu)
  994. {
  995. struct kfd_topology_device *dev;
  996. struct kfd_topology_device *out_dev = NULL;
  997. struct kfd_mem_properties *mem;
  998. struct kfd_cache_properties *cache;
  999. struct kfd_iolink_properties *iolink;
  1000. struct kfd_iolink_properties *p2plink;
  1001. list_for_each_entry(dev, &topology_device_list, list) {
  1002. /* Discrete GPUs need their own topology device list
  1003. * entries. Don't assign them to CPU/APU nodes.
  1004. */
  1005. if (dev->node_props.cpu_cores_count)
  1006. continue;
  1007. if (!dev->gpu && (dev->node_props.simd_count > 0)) {
  1008. dev->gpu = gpu;
  1009. out_dev = dev;
  1010. list_for_each_entry(mem, &dev->mem_props, list)
  1011. mem->gpu = dev->gpu;
  1012. list_for_each_entry(cache, &dev->cache_props, list)
  1013. cache->gpu = dev->gpu;
  1014. list_for_each_entry(iolink, &dev->io_link_props, list)
  1015. iolink->gpu = dev->gpu;
  1016. list_for_each_entry(p2plink, &dev->p2p_link_props, list)
  1017. p2plink->gpu = dev->gpu;
  1018. break;
  1019. }
  1020. }
  1021. return out_dev;
  1022. }
  1023. static void kfd_notify_gpu_change(uint32_t gpu_id, int arrival)
  1024. {
  1025. /*
  1026. * TODO: Generate an event for thunk about the arrival/removal
  1027. * of the GPU
  1028. */
  1029. }
  1030. /* kfd_fill_mem_clk_max_info - Since CRAT doesn't have memory clock info,
  1031. * patch this after CRAT parsing.
  1032. */
  1033. static void kfd_fill_mem_clk_max_info(struct kfd_topology_device *dev)
  1034. {
  1035. struct kfd_mem_properties *mem;
  1036. struct kfd_local_mem_info local_mem_info;
  1037. if (!dev)
  1038. return;
  1039. /* Currently, amdgpu driver (amdgpu_mc) deals only with GPUs with
  1040. * single bank of VRAM local memory.
  1041. * for dGPUs - VCRAT reports only one bank of Local Memory
  1042. * for APUs - If CRAT from ACPI reports more than one bank, then
  1043. * all the banks will report the same mem_clk_max information
  1044. */
  1045. amdgpu_amdkfd_get_local_mem_info(dev->gpu->adev, &local_mem_info,
  1046. dev->gpu->xcp);
  1047. list_for_each_entry(mem, &dev->mem_props, list)
  1048. mem->mem_clk_max = local_mem_info.mem_clk_max;
  1049. }
  1050. static void kfd_set_iolink_no_atomics(struct kfd_topology_device *dev,
  1051. struct kfd_topology_device *target_gpu_dev,
  1052. struct kfd_iolink_properties *link)
  1053. {
  1054. /* xgmi always supports atomics between links. */
  1055. if (link->iolink_type == CRAT_IOLINK_TYPE_XGMI)
  1056. return;
  1057. /* check pcie support to set cpu(dev) flags for target_gpu_dev link. */
  1058. if (target_gpu_dev) {
  1059. uint32_t cap;
  1060. pcie_capability_read_dword(target_gpu_dev->gpu->adev->pdev,
  1061. PCI_EXP_DEVCAP2, &cap);
  1062. if (!(cap & (PCI_EXP_DEVCAP2_ATOMIC_COMP32 |
  1063. PCI_EXP_DEVCAP2_ATOMIC_COMP64)))
  1064. link->flags |= CRAT_IOLINK_FLAGS_NO_ATOMICS_32_BIT |
  1065. CRAT_IOLINK_FLAGS_NO_ATOMICS_64_BIT;
  1066. /* set gpu (dev) flags. */
  1067. } else {
  1068. if (!dev->gpu->kfd->pci_atomic_requested ||
  1069. dev->gpu->adev->asic_type == CHIP_HAWAII)
  1070. link->flags |= CRAT_IOLINK_FLAGS_NO_ATOMICS_32_BIT |
  1071. CRAT_IOLINK_FLAGS_NO_ATOMICS_64_BIT;
  1072. }
  1073. }
  1074. static void kfd_set_iolink_non_coherent(struct kfd_topology_device *to_dev,
  1075. struct kfd_iolink_properties *outbound_link,
  1076. struct kfd_iolink_properties *inbound_link)
  1077. {
  1078. /* CPU -> GPU with PCIe */
  1079. if (!to_dev->gpu &&
  1080. inbound_link->iolink_type == CRAT_IOLINK_TYPE_PCIEXPRESS)
  1081. inbound_link->flags |= CRAT_IOLINK_FLAGS_NON_COHERENT;
  1082. if (to_dev->gpu) {
  1083. /* GPU <-> GPU with PCIe and
  1084. * Vega20 with XGMI
  1085. */
  1086. if (inbound_link->iolink_type == CRAT_IOLINK_TYPE_PCIEXPRESS ||
  1087. (inbound_link->iolink_type == CRAT_IOLINK_TYPE_XGMI &&
  1088. KFD_GC_VERSION(to_dev->gpu) == IP_VERSION(9, 4, 0))) {
  1089. outbound_link->flags |= CRAT_IOLINK_FLAGS_NON_COHERENT;
  1090. inbound_link->flags |= CRAT_IOLINK_FLAGS_NON_COHERENT;
  1091. }
  1092. }
  1093. }
  1094. #define REC_SDMA_NUM_GPU 8
  1095. static const int rec_sdma_eng_map[REC_SDMA_NUM_GPU][REC_SDMA_NUM_GPU] = {
  1096. { -1, 14, 12, 2, 4, 8, 10, 6 },
  1097. { 14, -1, 2, 10, 8, 4, 6, 12 },
  1098. { 10, 2, -1, 12, 14, 6, 4, 8 },
  1099. { 2, 12, 10, -1, 6, 14, 8, 4 },
  1100. { 4, 8, 14, 6, -1, 10, 12, 2 },
  1101. { 8, 4, 6, 14, 12, -1, 2, 10 },
  1102. { 10, 6, 4, 8, 12, 2, -1, 14 },
  1103. { 6, 12, 8, 4, 2, 10, 14, -1 }};
  1104. static void kfd_set_recommended_sdma_engines(struct kfd_topology_device *to_dev,
  1105. struct kfd_iolink_properties *outbound_link,
  1106. struct kfd_iolink_properties *inbound_link)
  1107. {
  1108. struct kfd_node *gpu = outbound_link->gpu;
  1109. struct amdgpu_device *adev = gpu->adev;
  1110. int num_xgmi_nodes = adev->gmc.xgmi.num_physical_nodes;
  1111. bool support_rec_eng = !amdgpu_sriov_vf(adev) && to_dev->gpu &&
  1112. adev->aid_mask && num_xgmi_nodes && gpu->kfd->num_nodes == 1 &&
  1113. kfd_get_num_xgmi_sdma_engines(gpu) >= 14 &&
  1114. (!(adev->flags & AMD_IS_APU) && num_xgmi_nodes == 8);
  1115. if (support_rec_eng) {
  1116. int src_socket_id = adev->gmc.xgmi.physical_node_id;
  1117. int dst_socket_id = to_dev->gpu->adev->gmc.xgmi.physical_node_id;
  1118. outbound_link->rec_sdma_eng_id_mask =
  1119. 1 << rec_sdma_eng_map[src_socket_id][dst_socket_id];
  1120. inbound_link->rec_sdma_eng_id_mask =
  1121. 1 << rec_sdma_eng_map[dst_socket_id][src_socket_id];
  1122. } else {
  1123. int num_sdma_eng = kfd_get_num_sdma_engines(gpu);
  1124. int i, eng_offset = 0;
  1125. if (outbound_link->iolink_type == CRAT_IOLINK_TYPE_XGMI &&
  1126. kfd_get_num_xgmi_sdma_engines(gpu) && to_dev->gpu) {
  1127. eng_offset = num_sdma_eng;
  1128. num_sdma_eng = kfd_get_num_xgmi_sdma_engines(gpu);
  1129. }
  1130. for (i = 0; i < num_sdma_eng; i++) {
  1131. outbound_link->rec_sdma_eng_id_mask |= (1 << (i + eng_offset));
  1132. inbound_link->rec_sdma_eng_id_mask |= (1 << (i + eng_offset));
  1133. }
  1134. }
  1135. }
  1136. static void kfd_fill_iolink_non_crat_info(struct kfd_topology_device *dev)
  1137. {
  1138. struct kfd_iolink_properties *link, *inbound_link;
  1139. struct kfd_topology_device *peer_dev;
  1140. if (!dev || !dev->gpu)
  1141. return;
  1142. /* GPU only creates direct links so apply flags setting to all */
  1143. list_for_each_entry(link, &dev->io_link_props, list) {
  1144. link->flags = CRAT_IOLINK_FLAGS_ENABLED;
  1145. kfd_set_iolink_no_atomics(dev, NULL, link);
  1146. peer_dev = kfd_topology_device_by_proximity_domain(
  1147. link->node_to);
  1148. if (!peer_dev)
  1149. continue;
  1150. /* Include the CPU peer in GPU hive if connected over xGMI. */
  1151. if (!peer_dev->gpu &&
  1152. link->iolink_type == CRAT_IOLINK_TYPE_XGMI) {
  1153. /*
  1154. * If the GPU is not part of a GPU hive, use its pci
  1155. * device location as the hive ID to bind with the CPU.
  1156. */
  1157. if (!dev->node_props.hive_id)
  1158. dev->node_props.hive_id = pci_dev_id(dev->gpu->adev->pdev);
  1159. peer_dev->node_props.hive_id = dev->node_props.hive_id;
  1160. }
  1161. list_for_each_entry(inbound_link, &peer_dev->io_link_props,
  1162. list) {
  1163. if (inbound_link->node_to != link->node_from)
  1164. continue;
  1165. inbound_link->flags = CRAT_IOLINK_FLAGS_ENABLED;
  1166. kfd_set_iolink_no_atomics(peer_dev, dev, inbound_link);
  1167. kfd_set_iolink_non_coherent(peer_dev, link, inbound_link);
  1168. kfd_set_recommended_sdma_engines(peer_dev, link, inbound_link);
  1169. }
  1170. }
  1171. /* Create indirect links so apply flags setting to all */
  1172. list_for_each_entry(link, &dev->p2p_link_props, list) {
  1173. link->flags = CRAT_IOLINK_FLAGS_ENABLED;
  1174. kfd_set_iolink_no_atomics(dev, NULL, link);
  1175. peer_dev = kfd_topology_device_by_proximity_domain(
  1176. link->node_to);
  1177. if (!peer_dev)
  1178. continue;
  1179. list_for_each_entry(inbound_link, &peer_dev->p2p_link_props,
  1180. list) {
  1181. if (inbound_link->node_to != link->node_from)
  1182. continue;
  1183. inbound_link->flags = CRAT_IOLINK_FLAGS_ENABLED;
  1184. kfd_set_iolink_no_atomics(peer_dev, dev, inbound_link);
  1185. kfd_set_iolink_non_coherent(peer_dev, link, inbound_link);
  1186. }
  1187. }
  1188. }
  1189. static int kfd_build_p2p_node_entry(struct kfd_topology_device *dev,
  1190. struct kfd_iolink_properties *p2plink)
  1191. {
  1192. int ret;
  1193. p2plink->kobj = kzalloc(sizeof(struct kobject), GFP_KERNEL);
  1194. if (!p2plink->kobj)
  1195. return -ENOMEM;
  1196. ret = kobject_init_and_add(p2plink->kobj, &iolink_type,
  1197. dev->kobj_p2plink, "%d", dev->node_props.p2p_links_count - 1);
  1198. if (ret < 0) {
  1199. kobject_put(p2plink->kobj);
  1200. return ret;
  1201. }
  1202. p2plink->attr.name = "properties";
  1203. p2plink->attr.mode = KFD_SYSFS_FILE_MODE;
  1204. sysfs_attr_init(&p2plink->attr);
  1205. ret = sysfs_create_file(p2plink->kobj, &p2plink->attr);
  1206. if (ret < 0)
  1207. return ret;
  1208. return 0;
  1209. }
  1210. static int kfd_create_indirect_link_prop(struct kfd_topology_device *kdev, int gpu_node)
  1211. {
  1212. struct kfd_iolink_properties *gpu_link, *tmp_link, *cpu_link;
  1213. struct kfd_iolink_properties *props = NULL, *props2 = NULL;
  1214. struct kfd_topology_device *cpu_dev;
  1215. int ret = 0;
  1216. int i, num_cpu;
  1217. num_cpu = 0;
  1218. list_for_each_entry(cpu_dev, &topology_device_list, list) {
  1219. if (cpu_dev->gpu)
  1220. break;
  1221. num_cpu++;
  1222. }
  1223. if (list_empty(&kdev->io_link_props))
  1224. return -ENODATA;
  1225. gpu_link = list_first_entry(&kdev->io_link_props,
  1226. struct kfd_iolink_properties, list);
  1227. for (i = 0; i < num_cpu; i++) {
  1228. /* CPU <--> GPU */
  1229. if (gpu_link->node_to == i)
  1230. continue;
  1231. /* find CPU <--> CPU links */
  1232. cpu_link = NULL;
  1233. cpu_dev = kfd_topology_device_by_proximity_domain(i);
  1234. if (cpu_dev) {
  1235. list_for_each_entry(tmp_link,
  1236. &cpu_dev->io_link_props, list) {
  1237. if (tmp_link->node_to == gpu_link->node_to) {
  1238. cpu_link = tmp_link;
  1239. break;
  1240. }
  1241. }
  1242. }
  1243. if (!cpu_link)
  1244. return -ENOMEM;
  1245. /* CPU <--> CPU <--> GPU, GPU node*/
  1246. props = kfd_alloc_struct(props);
  1247. if (!props)
  1248. return -ENOMEM;
  1249. memcpy(props, gpu_link, sizeof(struct kfd_iolink_properties));
  1250. props->weight = gpu_link->weight + cpu_link->weight;
  1251. props->min_latency = gpu_link->min_latency + cpu_link->min_latency;
  1252. props->max_latency = gpu_link->max_latency + cpu_link->max_latency;
  1253. props->min_bandwidth = min(gpu_link->min_bandwidth, cpu_link->min_bandwidth);
  1254. props->max_bandwidth = min(gpu_link->max_bandwidth, cpu_link->max_bandwidth);
  1255. props->node_from = gpu_node;
  1256. props->node_to = i;
  1257. kdev->node_props.p2p_links_count++;
  1258. list_add_tail(&props->list, &kdev->p2p_link_props);
  1259. ret = kfd_build_p2p_node_entry(kdev, props);
  1260. if (ret < 0)
  1261. return ret;
  1262. /* for small Bar, no CPU --> GPU in-direct links */
  1263. if (kfd_dev_is_large_bar(kdev->gpu)) {
  1264. /* CPU <--> CPU <--> GPU, CPU node*/
  1265. props2 = kfd_alloc_struct(props2);
  1266. if (!props2)
  1267. return -ENOMEM;
  1268. memcpy(props2, props, sizeof(struct kfd_iolink_properties));
  1269. props2->node_from = i;
  1270. props2->node_to = gpu_node;
  1271. props2->kobj = NULL;
  1272. cpu_dev->node_props.p2p_links_count++;
  1273. list_add_tail(&props2->list, &cpu_dev->p2p_link_props);
  1274. ret = kfd_build_p2p_node_entry(cpu_dev, props2);
  1275. if (ret < 0)
  1276. return ret;
  1277. }
  1278. }
  1279. return ret;
  1280. }
  1281. #if defined(CONFIG_HSA_AMD_P2P)
  1282. static int kfd_add_peer_prop(struct kfd_topology_device *kdev,
  1283. struct kfd_topology_device *peer, int from, int to)
  1284. {
  1285. struct kfd_iolink_properties *props = NULL;
  1286. struct kfd_iolink_properties *iolink1, *iolink2, *iolink3;
  1287. struct kfd_topology_device *cpu_dev;
  1288. int ret = 0;
  1289. if (!amdgpu_device_is_peer_accessible(
  1290. kdev->gpu->adev,
  1291. peer->gpu->adev))
  1292. return ret;
  1293. if (list_empty(&kdev->io_link_props))
  1294. return -ENODATA;
  1295. iolink1 = list_first_entry(&kdev->io_link_props,
  1296. struct kfd_iolink_properties, list);
  1297. if (list_empty(&peer->io_link_props))
  1298. return -ENODATA;
  1299. iolink2 = list_first_entry(&peer->io_link_props,
  1300. struct kfd_iolink_properties, list);
  1301. props = kfd_alloc_struct(props);
  1302. if (!props)
  1303. return -ENOMEM;
  1304. memcpy(props, iolink1, sizeof(struct kfd_iolink_properties));
  1305. props->weight = iolink1->weight + iolink2->weight;
  1306. props->min_latency = iolink1->min_latency + iolink2->min_latency;
  1307. props->max_latency = iolink1->max_latency + iolink2->max_latency;
  1308. props->min_bandwidth = min(iolink1->min_bandwidth, iolink2->min_bandwidth);
  1309. props->max_bandwidth = min(iolink2->max_bandwidth, iolink2->max_bandwidth);
  1310. if (iolink1->node_to != iolink2->node_to) {
  1311. /* CPU->CPU link*/
  1312. cpu_dev = kfd_topology_device_by_proximity_domain(iolink1->node_to);
  1313. if (cpu_dev) {
  1314. list_for_each_entry(iolink3, &cpu_dev->io_link_props, list) {
  1315. if (iolink3->node_to != iolink2->node_to)
  1316. continue;
  1317. props->weight += iolink3->weight;
  1318. props->min_latency += iolink3->min_latency;
  1319. props->max_latency += iolink3->max_latency;
  1320. props->min_bandwidth = min(props->min_bandwidth,
  1321. iolink3->min_bandwidth);
  1322. props->max_bandwidth = min(props->max_bandwidth,
  1323. iolink3->max_bandwidth);
  1324. break;
  1325. }
  1326. } else {
  1327. WARN(1, "CPU node not found");
  1328. }
  1329. }
  1330. props->node_from = from;
  1331. props->node_to = to;
  1332. peer->node_props.p2p_links_count++;
  1333. list_add_tail(&props->list, &peer->p2p_link_props);
  1334. ret = kfd_build_p2p_node_entry(peer, props);
  1335. return ret;
  1336. }
  1337. #endif
  1338. static int kfd_dev_create_p2p_links(void)
  1339. {
  1340. struct kfd_topology_device *dev;
  1341. struct kfd_topology_device *new_dev;
  1342. #if defined(CONFIG_HSA_AMD_P2P)
  1343. uint32_t i;
  1344. #endif
  1345. uint32_t k;
  1346. int ret = 0;
  1347. k = 0;
  1348. list_for_each_entry(dev, &topology_device_list, list)
  1349. k++;
  1350. if (k < 2)
  1351. return 0;
  1352. new_dev = list_last_entry(&topology_device_list, struct kfd_topology_device, list);
  1353. if (WARN_ON(!new_dev->gpu))
  1354. return 0;
  1355. k--;
  1356. /* create in-direct links */
  1357. ret = kfd_create_indirect_link_prop(new_dev, k);
  1358. if (ret < 0)
  1359. goto out;
  1360. /* create p2p links */
  1361. #if defined(CONFIG_HSA_AMD_P2P)
  1362. i = 0;
  1363. list_for_each_entry(dev, &topology_device_list, list) {
  1364. if (dev == new_dev)
  1365. break;
  1366. if (!dev->gpu || !dev->gpu->adev ||
  1367. (dev->gpu->kfd->hive_id &&
  1368. dev->gpu->kfd->hive_id == new_dev->gpu->kfd->hive_id))
  1369. goto next;
  1370. /* check if node(s) is/are peer accessible in one direction or bi-direction */
  1371. ret = kfd_add_peer_prop(new_dev, dev, i, k);
  1372. if (ret < 0)
  1373. goto out;
  1374. ret = kfd_add_peer_prop(dev, new_dev, k, i);
  1375. if (ret < 0)
  1376. goto out;
  1377. next:
  1378. i++;
  1379. }
  1380. #endif
  1381. out:
  1382. return ret;
  1383. }
  1384. /* Helper function. See kfd_fill_gpu_cache_info for parameter description */
  1385. static int fill_in_l1_pcache(struct kfd_cache_properties **props_ext,
  1386. struct kfd_gpu_cache_info *pcache_info,
  1387. int cu_bitmask,
  1388. int cache_type, unsigned int cu_processor_id,
  1389. int cu_block)
  1390. {
  1391. unsigned int cu_sibling_map_mask;
  1392. int first_active_cu;
  1393. struct kfd_cache_properties *pcache = NULL;
  1394. cu_sibling_map_mask = cu_bitmask;
  1395. cu_sibling_map_mask >>= cu_block;
  1396. cu_sibling_map_mask &= ((1 << pcache_info[cache_type].num_cu_shared) - 1);
  1397. first_active_cu = ffs(cu_sibling_map_mask);
  1398. /* CU could be inactive. In case of shared cache find the first active
  1399. * CU. and incase of non-shared cache check if the CU is inactive. If
  1400. * inactive active skip it
  1401. */
  1402. if (first_active_cu) {
  1403. pcache = kfd_alloc_struct(pcache);
  1404. if (!pcache)
  1405. return -ENOMEM;
  1406. memset(pcache, 0, sizeof(struct kfd_cache_properties));
  1407. pcache->processor_id_low = cu_processor_id + (first_active_cu - 1);
  1408. pcache->cache_level = pcache_info[cache_type].cache_level;
  1409. pcache->cache_size = pcache_info[cache_type].cache_size;
  1410. pcache->cacheline_size = pcache_info[cache_type].cache_line_size;
  1411. if (pcache_info[cache_type].flags & CRAT_CACHE_FLAGS_DATA_CACHE)
  1412. pcache->cache_type |= HSA_CACHE_TYPE_DATA;
  1413. if (pcache_info[cache_type].flags & CRAT_CACHE_FLAGS_INST_CACHE)
  1414. pcache->cache_type |= HSA_CACHE_TYPE_INSTRUCTION;
  1415. if (pcache_info[cache_type].flags & CRAT_CACHE_FLAGS_CPU_CACHE)
  1416. pcache->cache_type |= HSA_CACHE_TYPE_CPU;
  1417. if (pcache_info[cache_type].flags & CRAT_CACHE_FLAGS_SIMD_CACHE)
  1418. pcache->cache_type |= HSA_CACHE_TYPE_HSACU;
  1419. /* Sibling map is w.r.t processor_id_low, so shift out
  1420. * inactive CU
  1421. */
  1422. cu_sibling_map_mask =
  1423. cu_sibling_map_mask >> (first_active_cu - 1);
  1424. pcache->sibling_map[0] = (uint8_t)(cu_sibling_map_mask & 0xFF);
  1425. pcache->sibling_map[1] =
  1426. (uint8_t)((cu_sibling_map_mask >> 8) & 0xFF);
  1427. pcache->sibling_map[2] =
  1428. (uint8_t)((cu_sibling_map_mask >> 16) & 0xFF);
  1429. pcache->sibling_map[3] =
  1430. (uint8_t)((cu_sibling_map_mask >> 24) & 0xFF);
  1431. pcache->sibling_map_size = 4;
  1432. *props_ext = pcache;
  1433. return 0;
  1434. }
  1435. return 1;
  1436. }
  1437. /* Helper function. See kfd_fill_gpu_cache_info for parameter description */
  1438. static int fill_in_l2_l3_pcache(struct kfd_cache_properties **props_ext,
  1439. struct kfd_gpu_cache_info *pcache_info,
  1440. struct amdgpu_cu_info *cu_info,
  1441. struct amdgpu_gfx_config *gfx_info,
  1442. int cache_type, unsigned int cu_processor_id,
  1443. struct kfd_node *knode)
  1444. {
  1445. unsigned int cu_sibling_map_mask = 0;
  1446. int first_active_cu;
  1447. int i, j, k, xcc, start, end;
  1448. int num_xcc = NUM_XCC(knode->xcc_mask);
  1449. struct kfd_cache_properties *pcache = NULL;
  1450. enum amdgpu_memory_partition mode;
  1451. struct amdgpu_device *adev = knode->adev;
  1452. bool found = false;
  1453. start = ffs(knode->xcc_mask) - 1;
  1454. end = start + num_xcc;
  1455. /* To find the bitmap in the first active cu in the first
  1456. * xcc, it is based on the assumption that evrey xcc must
  1457. * have at least one active cu.
  1458. */
  1459. for (i = 0; i < gfx_info->max_shader_engines && !found; i++) {
  1460. for (j = 0; j < gfx_info->max_sh_per_se && !found; j++) {
  1461. if (cu_info->bitmap[start][i % 4][j % 4]) {
  1462. cu_sibling_map_mask =
  1463. cu_info->bitmap[start][i % 4][j % 4];
  1464. found = true;
  1465. }
  1466. }
  1467. }
  1468. cu_sibling_map_mask &=
  1469. ((1 << pcache_info[cache_type].num_cu_shared) - 1);
  1470. first_active_cu = ffs(cu_sibling_map_mask);
  1471. /* CU could be inactive. In case of shared cache find the first active
  1472. * CU. and incase of non-shared cache check if the CU is inactive. If
  1473. * inactive active skip it
  1474. */
  1475. if (first_active_cu) {
  1476. pcache = kfd_alloc_struct(pcache);
  1477. if (!pcache)
  1478. return -ENOMEM;
  1479. memset(pcache, 0, sizeof(struct kfd_cache_properties));
  1480. pcache->processor_id_low = cu_processor_id
  1481. + (first_active_cu - 1);
  1482. pcache->cache_level = pcache_info[cache_type].cache_level;
  1483. pcache->cacheline_size = pcache_info[cache_type].cache_line_size;
  1484. if (KFD_GC_VERSION(knode) == IP_VERSION(9, 4, 3) ||
  1485. KFD_GC_VERSION(knode) == IP_VERSION(9, 4, 4))
  1486. mode = adev->gmc.gmc_funcs->query_mem_partition_mode(adev);
  1487. else
  1488. mode = UNKNOWN_MEMORY_PARTITION_MODE;
  1489. pcache->cache_size = pcache_info[cache_type].cache_size;
  1490. /* Partition mode only affects L3 cache size */
  1491. if (mode && pcache->cache_level == 3)
  1492. pcache->cache_size /= mode;
  1493. if (pcache_info[cache_type].flags & CRAT_CACHE_FLAGS_DATA_CACHE)
  1494. pcache->cache_type |= HSA_CACHE_TYPE_DATA;
  1495. if (pcache_info[cache_type].flags & CRAT_CACHE_FLAGS_INST_CACHE)
  1496. pcache->cache_type |= HSA_CACHE_TYPE_INSTRUCTION;
  1497. if (pcache_info[cache_type].flags & CRAT_CACHE_FLAGS_CPU_CACHE)
  1498. pcache->cache_type |= HSA_CACHE_TYPE_CPU;
  1499. if (pcache_info[cache_type].flags & CRAT_CACHE_FLAGS_SIMD_CACHE)
  1500. pcache->cache_type |= HSA_CACHE_TYPE_HSACU;
  1501. /* Sibling map is w.r.t processor_id_low, so shift out
  1502. * inactive CU
  1503. */
  1504. cu_sibling_map_mask = cu_sibling_map_mask >> (first_active_cu - 1);
  1505. k = 0;
  1506. for (xcc = start; xcc < end; xcc++) {
  1507. for (i = 0; i < gfx_info->max_shader_engines; i++) {
  1508. for (j = 0; j < gfx_info->max_sh_per_se; j++) {
  1509. pcache->sibling_map[k] = (uint8_t)(cu_sibling_map_mask & 0xFF);
  1510. pcache->sibling_map[k+1] = (uint8_t)((cu_sibling_map_mask >> 8) & 0xFF);
  1511. pcache->sibling_map[k+2] = (uint8_t)((cu_sibling_map_mask >> 16) & 0xFF);
  1512. pcache->sibling_map[k+3] = (uint8_t)((cu_sibling_map_mask >> 24) & 0xFF);
  1513. k += 4;
  1514. cu_sibling_map_mask = cu_info->bitmap[xcc][i % 4][j + i / 4];
  1515. cu_sibling_map_mask &= ((1 << pcache_info[cache_type].num_cu_shared) - 1);
  1516. }
  1517. }
  1518. }
  1519. pcache->sibling_map_size = k;
  1520. *props_ext = pcache;
  1521. return 0;
  1522. }
  1523. return 1;
  1524. }
  1525. #define KFD_MAX_CACHE_TYPES 6
  1526. /* kfd_fill_cache_non_crat_info - Fill GPU cache info using kfd_gpu_cache_info
  1527. * tables
  1528. */
  1529. static void kfd_fill_cache_non_crat_info(struct kfd_topology_device *dev, struct kfd_node *kdev)
  1530. {
  1531. struct kfd_gpu_cache_info *pcache_info = NULL;
  1532. int i, j, k, xcc, start, end;
  1533. int ct = 0;
  1534. unsigned int cu_processor_id;
  1535. int ret;
  1536. unsigned int num_cu_shared;
  1537. struct amdgpu_cu_info *cu_info = &kdev->adev->gfx.cu_info;
  1538. struct amdgpu_gfx_config *gfx_info = &kdev->adev->gfx.config;
  1539. int gpu_processor_id;
  1540. struct kfd_cache_properties *props_ext;
  1541. int num_of_entries = 0;
  1542. int num_of_cache_types = 0;
  1543. struct kfd_gpu_cache_info cache_info[KFD_MAX_CACHE_TYPES];
  1544. gpu_processor_id = dev->node_props.simd_id_base;
  1545. memset(cache_info, 0, sizeof(cache_info));
  1546. pcache_info = cache_info;
  1547. num_of_cache_types = kfd_get_gpu_cache_info(kdev, &pcache_info);
  1548. if (!num_of_cache_types) {
  1549. pr_warn("no cache info found\n");
  1550. return;
  1551. }
  1552. /* For each type of cache listed in the kfd_gpu_cache_info table,
  1553. * go through all available Compute Units.
  1554. * The [i,j,k] loop will
  1555. * if kfd_gpu_cache_info.num_cu_shared = 1
  1556. * will parse through all available CU
  1557. * If (kfd_gpu_cache_info.num_cu_shared != 1)
  1558. * then it will consider only one CU from
  1559. * the shared unit
  1560. */
  1561. start = ffs(kdev->xcc_mask) - 1;
  1562. end = start + NUM_XCC(kdev->xcc_mask);
  1563. for (ct = 0; ct < num_of_cache_types; ct++) {
  1564. cu_processor_id = gpu_processor_id;
  1565. if (pcache_info[ct].cache_level == 1) {
  1566. for (xcc = start; xcc < end; xcc++) {
  1567. for (i = 0; i < gfx_info->max_shader_engines; i++) {
  1568. for (j = 0; j < gfx_info->max_sh_per_se; j++) {
  1569. for (k = 0; k < gfx_info->max_cu_per_sh; k += pcache_info[ct].num_cu_shared) {
  1570. ret = fill_in_l1_pcache(&props_ext, pcache_info,
  1571. cu_info->bitmap[xcc][i % 4][j + i / 4], ct,
  1572. cu_processor_id, k);
  1573. if (ret < 0)
  1574. break;
  1575. if (!ret) {
  1576. num_of_entries++;
  1577. list_add_tail(&props_ext->list, &dev->cache_props);
  1578. }
  1579. /* Move to next CU block */
  1580. num_cu_shared = ((k + pcache_info[ct].num_cu_shared) <=
  1581. gfx_info->max_cu_per_sh) ?
  1582. pcache_info[ct].num_cu_shared :
  1583. (gfx_info->max_cu_per_sh - k);
  1584. cu_processor_id += num_cu_shared;
  1585. }
  1586. }
  1587. }
  1588. }
  1589. } else {
  1590. ret = fill_in_l2_l3_pcache(&props_ext, pcache_info,
  1591. cu_info, gfx_info, ct, cu_processor_id, kdev);
  1592. if (ret < 0)
  1593. break;
  1594. if (!ret) {
  1595. num_of_entries++;
  1596. list_add_tail(&props_ext->list, &dev->cache_props);
  1597. }
  1598. }
  1599. }
  1600. dev->node_props.caches_count += num_of_entries;
  1601. pr_debug("Added [%d] GPU cache entries\n", num_of_entries);
  1602. }
  1603. static int kfd_topology_add_device_locked(struct kfd_node *gpu,
  1604. struct kfd_topology_device **dev)
  1605. {
  1606. int proximity_domain = ++topology_crat_proximity_domain;
  1607. struct list_head temp_topology_device_list;
  1608. void *crat_image = NULL;
  1609. size_t image_size = 0;
  1610. int res;
  1611. res = kfd_create_crat_image_virtual(&crat_image, &image_size,
  1612. COMPUTE_UNIT_GPU, gpu,
  1613. proximity_domain);
  1614. if (res) {
  1615. dev_err(gpu->adev->dev, "Error creating VCRAT\n");
  1616. topology_crat_proximity_domain--;
  1617. goto err;
  1618. }
  1619. INIT_LIST_HEAD(&temp_topology_device_list);
  1620. res = kfd_parse_crat_table(crat_image,
  1621. &temp_topology_device_list,
  1622. proximity_domain);
  1623. if (res) {
  1624. dev_err(gpu->adev->dev, "Error parsing VCRAT\n");
  1625. topology_crat_proximity_domain--;
  1626. goto err;
  1627. }
  1628. kfd_topology_update_device_list(&temp_topology_device_list,
  1629. &topology_device_list);
  1630. *dev = kfd_assign_gpu(gpu);
  1631. if (WARN_ON(!*dev)) {
  1632. res = -ENODEV;
  1633. goto err;
  1634. }
  1635. /* Fill the cache affinity information here for the GPUs
  1636. * using VCRAT
  1637. */
  1638. kfd_fill_cache_non_crat_info(*dev, gpu);
  1639. /* Update the SYSFS tree, since we added another topology
  1640. * device
  1641. */
  1642. res = kfd_topology_update_sysfs();
  1643. if (!res)
  1644. sys_props.generation_count++;
  1645. else
  1646. dev_err(gpu->adev->dev, "Failed to update GPU to sysfs topology. res=%d\n",
  1647. res);
  1648. err:
  1649. kfd_destroy_crat_image(crat_image);
  1650. return res;
  1651. }
  1652. static void kfd_topology_set_dbg_firmware_support(struct kfd_topology_device *dev)
  1653. {
  1654. bool firmware_supported = true;
  1655. if (KFD_GC_VERSION(dev->gpu) >= IP_VERSION(11, 0, 0) &&
  1656. KFD_GC_VERSION(dev->gpu) < IP_VERSION(12, 0, 0)) {
  1657. uint32_t mes_api_rev = (dev->gpu->adev->mes.sched_version &
  1658. AMDGPU_MES_API_VERSION_MASK) >>
  1659. AMDGPU_MES_API_VERSION_SHIFT;
  1660. uint32_t mes_rev = dev->gpu->adev->mes.sched_version &
  1661. AMDGPU_MES_VERSION_MASK;
  1662. firmware_supported = (mes_api_rev >= 14) && (mes_rev >= 64);
  1663. goto out;
  1664. }
  1665. /*
  1666. * Note: Any unlisted devices here are assumed to support exception handling.
  1667. * Add additional checks here as needed.
  1668. */
  1669. switch (KFD_GC_VERSION(dev->gpu)) {
  1670. case IP_VERSION(9, 0, 1):
  1671. firmware_supported = dev->gpu->kfd->mec_fw_version >= 459 + 32768;
  1672. break;
  1673. case IP_VERSION(9, 1, 0):
  1674. case IP_VERSION(9, 2, 1):
  1675. case IP_VERSION(9, 2, 2):
  1676. case IP_VERSION(9, 3, 0):
  1677. case IP_VERSION(9, 4, 0):
  1678. firmware_supported = dev->gpu->kfd->mec_fw_version >= 459;
  1679. break;
  1680. case IP_VERSION(9, 4, 1):
  1681. firmware_supported = dev->gpu->kfd->mec_fw_version >= 60;
  1682. break;
  1683. case IP_VERSION(9, 4, 2):
  1684. firmware_supported = dev->gpu->kfd->mec_fw_version >= 51;
  1685. break;
  1686. case IP_VERSION(10, 1, 10):
  1687. case IP_VERSION(10, 1, 2):
  1688. case IP_VERSION(10, 1, 1):
  1689. firmware_supported = dev->gpu->kfd->mec_fw_version >= 144;
  1690. break;
  1691. case IP_VERSION(10, 3, 0):
  1692. case IP_VERSION(10, 3, 2):
  1693. case IP_VERSION(10, 3, 1):
  1694. case IP_VERSION(10, 3, 4):
  1695. case IP_VERSION(10, 3, 5):
  1696. firmware_supported = dev->gpu->kfd->mec_fw_version >= 89;
  1697. break;
  1698. case IP_VERSION(10, 1, 3):
  1699. case IP_VERSION(10, 3, 3):
  1700. firmware_supported = false;
  1701. break;
  1702. default:
  1703. break;
  1704. }
  1705. out:
  1706. if (firmware_supported)
  1707. dev->node_props.capability |= HSA_CAP_TRAP_DEBUG_FIRMWARE_SUPPORTED;
  1708. }
  1709. static void kfd_topology_set_capabilities(struct kfd_topology_device *dev)
  1710. {
  1711. dev->node_props.capability |= ((HSA_CAP_DOORBELL_TYPE_2_0 <<
  1712. HSA_CAP_DOORBELL_TYPE_TOTALBITS_SHIFT) &
  1713. HSA_CAP_DOORBELL_TYPE_TOTALBITS_MASK);
  1714. dev->node_props.capability |= HSA_CAP_TRAP_DEBUG_SUPPORT |
  1715. HSA_CAP_TRAP_DEBUG_WAVE_LAUNCH_TRAP_OVERRIDE_SUPPORTED |
  1716. HSA_CAP_TRAP_DEBUG_WAVE_LAUNCH_MODE_SUPPORTED;
  1717. if (kfd_dbg_has_ttmps_always_setup(dev->gpu))
  1718. dev->node_props.debug_prop |= HSA_DBG_DISPATCH_INFO_ALWAYS_VALID;
  1719. if (KFD_GC_VERSION(dev->gpu) < IP_VERSION(10, 0, 0)) {
  1720. if (KFD_GC_VERSION(dev->gpu) == IP_VERSION(9, 4, 3) ||
  1721. KFD_GC_VERSION(dev->gpu) == IP_VERSION(9, 4, 4))
  1722. dev->node_props.debug_prop |=
  1723. HSA_DBG_WATCH_ADDR_MASK_LO_BIT_GFX9_4_3 |
  1724. HSA_DBG_WATCH_ADDR_MASK_HI_BIT_GFX9_4_3;
  1725. else
  1726. dev->node_props.debug_prop |=
  1727. HSA_DBG_WATCH_ADDR_MASK_LO_BIT_GFX9 |
  1728. HSA_DBG_WATCH_ADDR_MASK_HI_BIT;
  1729. if (KFD_GC_VERSION(dev->gpu) >= IP_VERSION(9, 4, 2))
  1730. dev->node_props.capability |=
  1731. HSA_CAP_TRAP_DEBUG_PRECISE_MEMORY_OPERATIONS_SUPPORTED;
  1732. } else {
  1733. dev->node_props.debug_prop |= HSA_DBG_WATCH_ADDR_MASK_LO_BIT_GFX10 |
  1734. HSA_DBG_WATCH_ADDR_MASK_HI_BIT;
  1735. if (KFD_GC_VERSION(dev->gpu) >= IP_VERSION(12, 0, 0))
  1736. dev->node_props.capability |=
  1737. HSA_CAP_TRAP_DEBUG_PRECISE_ALU_OPERATIONS_SUPPORTED;
  1738. }
  1739. kfd_topology_set_dbg_firmware_support(dev);
  1740. }
  1741. int kfd_topology_add_device(struct kfd_node *gpu)
  1742. {
  1743. uint32_t gpu_id;
  1744. struct kfd_topology_device *dev;
  1745. int res = 0;
  1746. int i;
  1747. const char *asic_name = amdgpu_asic_name[gpu->adev->asic_type];
  1748. struct amdgpu_gfx_config *gfx_info = &gpu->adev->gfx.config;
  1749. struct amdgpu_cu_info *cu_info = &gpu->adev->gfx.cu_info;
  1750. if (gpu->xcp && !gpu->xcp->ddev) {
  1751. dev_warn(gpu->adev->dev,
  1752. "Won't add GPU to topology since it has no drm node assigned.");
  1753. return 0;
  1754. } else {
  1755. dev_dbg(gpu->adev->dev, "Adding new GPU to topology\n");
  1756. }
  1757. /* Check to see if this gpu device exists in the topology_device_list.
  1758. * If so, assign the gpu to that device,
  1759. * else create a Virtual CRAT for this gpu device and then parse that
  1760. * CRAT to create a new topology device. Once created assign the gpu to
  1761. * that topology device
  1762. */
  1763. down_write(&topology_lock);
  1764. dev = kfd_assign_gpu(gpu);
  1765. if (!dev)
  1766. res = kfd_topology_add_device_locked(gpu, &dev);
  1767. up_write(&topology_lock);
  1768. if (res)
  1769. return res;
  1770. gpu_id = kfd_generate_gpu_id(gpu);
  1771. dev->gpu_id = gpu_id;
  1772. gpu->id = gpu_id;
  1773. kfd_dev_create_p2p_links();
  1774. /* TODO: Move the following lines to function
  1775. * kfd_add_non_crat_information
  1776. */
  1777. /* Fill-in additional information that is not available in CRAT but
  1778. * needed for the topology
  1779. */
  1780. for (i = 0; i < KFD_TOPOLOGY_PUBLIC_NAME_SIZE-1; i++) {
  1781. dev->node_props.name[i] = __tolower(asic_name[i]);
  1782. if (asic_name[i] == '\0')
  1783. break;
  1784. }
  1785. dev->node_props.name[i] = '\0';
  1786. dev->node_props.simd_arrays_per_engine =
  1787. gfx_info->max_sh_per_se;
  1788. dev->node_props.gfx_target_version =
  1789. gpu->kfd->device_info.gfx_target_version;
  1790. dev->node_props.vendor_id = gpu->adev->pdev->vendor;
  1791. dev->node_props.device_id = gpu->adev->pdev->device;
  1792. dev->node_props.capability |=
  1793. ((dev->gpu->adev->rev_id << HSA_CAP_ASIC_REVISION_SHIFT) &
  1794. HSA_CAP_ASIC_REVISION_MASK);
  1795. dev->node_props.location_id = pci_dev_id(gpu->adev->pdev);
  1796. if (gpu->kfd->num_nodes > 1)
  1797. dev->node_props.location_id |= dev->gpu->node_id;
  1798. dev->node_props.domain = pci_domain_nr(gpu->adev->pdev->bus);
  1799. dev->node_props.max_engine_clk_fcompute =
  1800. amdgpu_amdkfd_get_max_engine_clock_in_mhz(dev->gpu->adev);
  1801. dev->node_props.max_engine_clk_ccompute =
  1802. cpufreq_quick_get_max(0) / 1000;
  1803. if (gpu->xcp)
  1804. dev->node_props.drm_render_minor = gpu->xcp->ddev->render->index;
  1805. else
  1806. dev->node_props.drm_render_minor =
  1807. gpu->kfd->shared_resources.drm_render_minor;
  1808. dev->node_props.hive_id = gpu->kfd->hive_id;
  1809. dev->node_props.num_sdma_engines = kfd_get_num_sdma_engines(gpu);
  1810. dev->node_props.num_sdma_xgmi_engines =
  1811. kfd_get_num_xgmi_sdma_engines(gpu);
  1812. dev->node_props.num_sdma_queues_per_engine =
  1813. gpu->kfd->device_info.num_sdma_queues_per_engine -
  1814. gpu->kfd->device_info.num_reserved_sdma_queues_per_engine;
  1815. dev->node_props.num_gws = (dev->gpu->gws &&
  1816. dev->gpu->dqm->sched_policy != KFD_SCHED_POLICY_NO_HWS) ?
  1817. dev->gpu->adev->gds.gws_size : 0;
  1818. dev->node_props.num_cp_queues = get_cp_queues_num(dev->gpu->dqm);
  1819. kfd_fill_mem_clk_max_info(dev);
  1820. kfd_fill_iolink_non_crat_info(dev);
  1821. switch (dev->gpu->adev->asic_type) {
  1822. case CHIP_KAVERI:
  1823. case CHIP_HAWAII:
  1824. case CHIP_TONGA:
  1825. dev->node_props.capability |= ((HSA_CAP_DOORBELL_TYPE_PRE_1_0 <<
  1826. HSA_CAP_DOORBELL_TYPE_TOTALBITS_SHIFT) &
  1827. HSA_CAP_DOORBELL_TYPE_TOTALBITS_MASK);
  1828. break;
  1829. case CHIP_CARRIZO:
  1830. case CHIP_FIJI:
  1831. case CHIP_POLARIS10:
  1832. case CHIP_POLARIS11:
  1833. case CHIP_POLARIS12:
  1834. case CHIP_VEGAM:
  1835. pr_debug("Adding doorbell packet type capability\n");
  1836. dev->node_props.capability |= ((HSA_CAP_DOORBELL_TYPE_1_0 <<
  1837. HSA_CAP_DOORBELL_TYPE_TOTALBITS_SHIFT) &
  1838. HSA_CAP_DOORBELL_TYPE_TOTALBITS_MASK);
  1839. break;
  1840. default:
  1841. if (KFD_GC_VERSION(dev->gpu) < IP_VERSION(9, 0, 1))
  1842. WARN(1, "Unexpected ASIC family %u",
  1843. dev->gpu->adev->asic_type);
  1844. else
  1845. kfd_topology_set_capabilities(dev);
  1846. }
  1847. /*
  1848. * Overwrite ATS capability according to needs_iommu_device to fix
  1849. * potential missing corresponding bit in CRAT of BIOS.
  1850. */
  1851. dev->node_props.capability &= ~HSA_CAP_ATS_PRESENT;
  1852. /* Fix errors in CZ CRAT.
  1853. * simd_count: Carrizo CRAT reports wrong simd_count, probably
  1854. * because it doesn't consider masked out CUs
  1855. * max_waves_per_simd: Carrizo reports wrong max_waves_per_simd
  1856. */
  1857. if (dev->gpu->adev->asic_type == CHIP_CARRIZO) {
  1858. dev->node_props.simd_count =
  1859. cu_info->simd_per_cu * cu_info->number;
  1860. dev->node_props.max_waves_per_simd = 10;
  1861. }
  1862. /* kfd only concerns sram ecc on GFX and HBM ecc on UMC */
  1863. dev->node_props.capability |=
  1864. ((dev->gpu->adev->ras_enabled & BIT(AMDGPU_RAS_BLOCK__GFX)) != 0) ?
  1865. HSA_CAP_SRAM_EDCSUPPORTED : 0;
  1866. dev->node_props.capability |=
  1867. ((dev->gpu->adev->ras_enabled & BIT(AMDGPU_RAS_BLOCK__UMC)) != 0) ?
  1868. HSA_CAP_MEM_EDCSUPPORTED : 0;
  1869. if (KFD_GC_VERSION(dev->gpu) != IP_VERSION(9, 0, 1))
  1870. dev->node_props.capability |= (dev->gpu->adev->ras_enabled != 0) ?
  1871. HSA_CAP_RASEVENTNOTIFY : 0;
  1872. if (KFD_IS_SVM_API_SUPPORTED(dev->gpu->adev))
  1873. dev->node_props.capability |= HSA_CAP_SVMAPI_SUPPORTED;
  1874. if (dev->gpu->adev->gmc.is_app_apu ||
  1875. dev->gpu->adev->gmc.xgmi.connected_to_cpu)
  1876. dev->node_props.capability |= HSA_CAP_FLAGS_COHERENTHOSTACCESS;
  1877. kfd_queue_ctx_save_restore_size(dev);
  1878. kfd_debug_print_topology();
  1879. kfd_notify_gpu_change(gpu_id, 1);
  1880. return 0;
  1881. }
  1882. /**
  1883. * kfd_topology_update_io_links() - Update IO links after device removal.
  1884. * @proximity_domain: Proximity domain value of the dev being removed.
  1885. *
  1886. * The topology list currently is arranged in increasing order of
  1887. * proximity domain.
  1888. *
  1889. * Two things need to be done when a device is removed:
  1890. * 1. All the IO links to this device need to be removed.
  1891. * 2. All nodes after the current device node need to move
  1892. * up once this device node is removed from the topology
  1893. * list. As a result, the proximity domain values for
  1894. * all nodes after the node being deleted reduce by 1.
  1895. * This would also cause the proximity domain values for
  1896. * io links to be updated based on new proximity domain
  1897. * values.
  1898. *
  1899. * Context: The caller must hold write topology_lock.
  1900. */
  1901. static void kfd_topology_update_io_links(int proximity_domain)
  1902. {
  1903. struct kfd_topology_device *dev;
  1904. struct kfd_iolink_properties *iolink, *p2plink, *tmp;
  1905. list_for_each_entry(dev, &topology_device_list, list) {
  1906. if (dev->proximity_domain > proximity_domain)
  1907. dev->proximity_domain--;
  1908. list_for_each_entry_safe(iolink, tmp, &dev->io_link_props, list) {
  1909. /*
  1910. * If there is an io link to the dev being deleted
  1911. * then remove that IO link also.
  1912. */
  1913. if (iolink->node_to == proximity_domain) {
  1914. list_del(&iolink->list);
  1915. dev->node_props.io_links_count--;
  1916. } else {
  1917. if (iolink->node_from > proximity_domain)
  1918. iolink->node_from--;
  1919. if (iolink->node_to > proximity_domain)
  1920. iolink->node_to--;
  1921. }
  1922. }
  1923. list_for_each_entry_safe(p2plink, tmp, &dev->p2p_link_props, list) {
  1924. /*
  1925. * If there is a p2p link to the dev being deleted
  1926. * then remove that p2p link also.
  1927. */
  1928. if (p2plink->node_to == proximity_domain) {
  1929. list_del(&p2plink->list);
  1930. dev->node_props.p2p_links_count--;
  1931. } else {
  1932. if (p2plink->node_from > proximity_domain)
  1933. p2plink->node_from--;
  1934. if (p2plink->node_to > proximity_domain)
  1935. p2plink->node_to--;
  1936. }
  1937. }
  1938. }
  1939. }
  1940. int kfd_topology_remove_device(struct kfd_node *gpu)
  1941. {
  1942. struct kfd_topology_device *dev, *tmp;
  1943. uint32_t gpu_id;
  1944. int res = -ENODEV;
  1945. int i = 0;
  1946. down_write(&topology_lock);
  1947. list_for_each_entry_safe(dev, tmp, &topology_device_list, list) {
  1948. if (dev->gpu == gpu) {
  1949. gpu_id = dev->gpu_id;
  1950. kfd_remove_sysfs_node_entry(dev);
  1951. kfd_release_topology_device(dev);
  1952. sys_props.num_devices--;
  1953. kfd_topology_update_io_links(i);
  1954. topology_crat_proximity_domain = sys_props.num_devices-1;
  1955. sys_props.generation_count++;
  1956. res = 0;
  1957. if (kfd_topology_update_sysfs() < 0)
  1958. kfd_topology_release_sysfs();
  1959. break;
  1960. }
  1961. i++;
  1962. }
  1963. up_write(&topology_lock);
  1964. if (!res)
  1965. kfd_notify_gpu_change(gpu_id, 0);
  1966. return res;
  1967. }
  1968. /* kfd_topology_enum_kfd_devices - Enumerate through all devices in KFD
  1969. * topology. If GPU device is found @idx, then valid kfd_dev pointer is
  1970. * returned through @kdev
  1971. * Return - 0: On success (@kdev will be NULL for non GPU nodes)
  1972. * -1: If end of list
  1973. */
  1974. int kfd_topology_enum_kfd_devices(uint8_t idx, struct kfd_node **kdev)
  1975. {
  1976. struct kfd_topology_device *top_dev;
  1977. uint8_t device_idx = 0;
  1978. *kdev = NULL;
  1979. down_read(&topology_lock);
  1980. list_for_each_entry(top_dev, &topology_device_list, list) {
  1981. if (device_idx == idx) {
  1982. *kdev = top_dev->gpu;
  1983. up_read(&topology_lock);
  1984. return 0;
  1985. }
  1986. device_idx++;
  1987. }
  1988. up_read(&topology_lock);
  1989. return -1;
  1990. }
  1991. static int kfd_cpumask_to_apic_id(const struct cpumask *cpumask)
  1992. {
  1993. int first_cpu_of_numa_node;
  1994. if (!cpumask || cpumask == cpu_none_mask)
  1995. return -1;
  1996. first_cpu_of_numa_node = cpumask_first(cpumask);
  1997. if (first_cpu_of_numa_node >= nr_cpu_ids)
  1998. return -1;
  1999. #ifdef CONFIG_X86_64
  2000. return cpu_data(first_cpu_of_numa_node).topo.apicid;
  2001. #else
  2002. return first_cpu_of_numa_node;
  2003. #endif
  2004. }
  2005. /* kfd_numa_node_to_apic_id - Returns the APIC ID of the first logical processor
  2006. * of the given NUMA node (numa_node_id)
  2007. * Return -1 on failure
  2008. */
  2009. int kfd_numa_node_to_apic_id(int numa_node_id)
  2010. {
  2011. if (numa_node_id == -1) {
  2012. pr_warn("Invalid NUMA Node. Use online CPU mask\n");
  2013. return kfd_cpumask_to_apic_id(cpu_online_mask);
  2014. }
  2015. return kfd_cpumask_to_apic_id(cpumask_of_node(numa_node_id));
  2016. }
  2017. #if defined(CONFIG_DEBUG_FS)
  2018. int kfd_debugfs_hqds_by_device(struct seq_file *m, void *data)
  2019. {
  2020. struct kfd_topology_device *dev;
  2021. unsigned int i = 0;
  2022. int r = 0;
  2023. down_read(&topology_lock);
  2024. list_for_each_entry(dev, &topology_device_list, list) {
  2025. if (!dev->gpu) {
  2026. i++;
  2027. continue;
  2028. }
  2029. seq_printf(m, "Node %u, gpu_id %x:\n", i++, dev->gpu->id);
  2030. r = dqm_debugfs_hqds(m, dev->gpu->dqm);
  2031. if (r)
  2032. break;
  2033. }
  2034. up_read(&topology_lock);
  2035. return r;
  2036. }
  2037. int kfd_debugfs_rls_by_device(struct seq_file *m, void *data)
  2038. {
  2039. struct kfd_topology_device *dev;
  2040. unsigned int i = 0;
  2041. int r = 0;
  2042. down_read(&topology_lock);
  2043. list_for_each_entry(dev, &topology_device_list, list) {
  2044. if (!dev->gpu) {
  2045. i++;
  2046. continue;
  2047. }
  2048. seq_printf(m, "Node %u, gpu_id %x:\n", i++, dev->gpu->id);
  2049. r = pm_debugfs_runlist(m, &dev->gpu->dqm->packet_mgr);
  2050. if (r)
  2051. break;
  2052. }
  2053. up_read(&topology_lock);
  2054. return r;
  2055. }
  2056. #endif