pci.c 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547
  1. /*
  2. * Copyright 2014 IBM Corp.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation; either version
  7. * 2 of the License, or (at your option) any later version.
  8. */
  9. #include <linux/pci_regs.h>
  10. #include <linux/pci_ids.h>
  11. #include <linux/device.h>
  12. #include <linux/module.h>
  13. #include <linux/kernel.h>
  14. #include <linux/slab.h>
  15. #include <linux/sort.h>
  16. #include <linux/pci.h>
  17. #include <linux/of.h>
  18. #include <linux/delay.h>
  19. #include <asm/opal.h>
  20. #include <asm/msi_bitmap.h>
  21. #include <asm/pci-bridge.h> /* for struct pci_controller */
  22. #include <asm/pnv-pci.h>
  23. #include <asm/io.h>
  24. #include "cxl.h"
  25. #include <misc/cxl.h>
  26. #define CXL_PCI_VSEC_ID 0x1280
  27. #define CXL_VSEC_MIN_SIZE 0x80
  28. #define CXL_READ_VSEC_LENGTH(dev, vsec, dest) \
  29. { \
  30. pci_read_config_word(dev, vsec + 0x6, dest); \
  31. *dest >>= 4; \
  32. }
  33. #define CXL_READ_VSEC_NAFUS(dev, vsec, dest) \
  34. pci_read_config_byte(dev, vsec + 0x8, dest)
  35. #define CXL_READ_VSEC_STATUS(dev, vsec, dest) \
  36. pci_read_config_byte(dev, vsec + 0x9, dest)
  37. #define CXL_STATUS_SECOND_PORT 0x80
  38. #define CXL_STATUS_MSI_X_FULL 0x40
  39. #define CXL_STATUS_MSI_X_SINGLE 0x20
  40. #define CXL_STATUS_FLASH_RW 0x08
  41. #define CXL_STATUS_FLASH_RO 0x04
  42. #define CXL_STATUS_LOADABLE_AFU 0x02
  43. #define CXL_STATUS_LOADABLE_PSL 0x01
  44. /* If we see these features we won't try to use the card */
  45. #define CXL_UNSUPPORTED_FEATURES \
  46. (CXL_STATUS_MSI_X_FULL | CXL_STATUS_MSI_X_SINGLE)
  47. #define CXL_READ_VSEC_MODE_CONTROL(dev, vsec, dest) \
  48. pci_read_config_byte(dev, vsec + 0xa, dest)
  49. #define CXL_WRITE_VSEC_MODE_CONTROL(dev, vsec, val) \
  50. pci_write_config_byte(dev, vsec + 0xa, val)
  51. #define CXL_VSEC_PROTOCOL_MASK 0xe0
  52. #define CXL_VSEC_PROTOCOL_1024TB 0x80
  53. #define CXL_VSEC_PROTOCOL_512TB 0x40
  54. #define CXL_VSEC_PROTOCOL_256TB 0x20 /* Power 8 uses this */
  55. #define CXL_VSEC_PROTOCOL_ENABLE 0x01
  56. #define CXL_READ_VSEC_PSL_REVISION(dev, vsec, dest) \
  57. pci_read_config_word(dev, vsec + 0xc, dest)
  58. #define CXL_READ_VSEC_CAIA_MINOR(dev, vsec, dest) \
  59. pci_read_config_byte(dev, vsec + 0xe, dest)
  60. #define CXL_READ_VSEC_CAIA_MAJOR(dev, vsec, dest) \
  61. pci_read_config_byte(dev, vsec + 0xf, dest)
  62. #define CXL_READ_VSEC_BASE_IMAGE(dev, vsec, dest) \
  63. pci_read_config_word(dev, vsec + 0x10, dest)
  64. #define CXL_READ_VSEC_IMAGE_STATE(dev, vsec, dest) \
  65. pci_read_config_byte(dev, vsec + 0x13, dest)
  66. #define CXL_WRITE_VSEC_IMAGE_STATE(dev, vsec, val) \
  67. pci_write_config_byte(dev, vsec + 0x13, val)
  68. #define CXL_VSEC_USER_IMAGE_LOADED 0x80 /* RO */
  69. #define CXL_VSEC_PERST_LOADS_IMAGE 0x20 /* RW */
  70. #define CXL_VSEC_PERST_SELECT_USER 0x10 /* RW */
  71. #define CXL_READ_VSEC_AFU_DESC_OFF(dev, vsec, dest) \
  72. pci_read_config_dword(dev, vsec + 0x20, dest)
  73. #define CXL_READ_VSEC_AFU_DESC_SIZE(dev, vsec, dest) \
  74. pci_read_config_dword(dev, vsec + 0x24, dest)
  75. #define CXL_READ_VSEC_PS_OFF(dev, vsec, dest) \
  76. pci_read_config_dword(dev, vsec + 0x28, dest)
  77. #define CXL_READ_VSEC_PS_SIZE(dev, vsec, dest) \
  78. pci_read_config_dword(dev, vsec + 0x2c, dest)
  79. /* This works a little different than the p1/p2 register accesses to make it
  80. * easier to pull out individual fields */
  81. #define AFUD_READ(afu, off) in_be64(afu->native->afu_desc_mmio + off)
  82. #define AFUD_READ_LE(afu, off) in_le64(afu->native->afu_desc_mmio + off)
  83. #define EXTRACT_PPC_BIT(val, bit) (!!(val & PPC_BIT(bit)))
  84. #define EXTRACT_PPC_BITS(val, bs, be) ((val & PPC_BITMASK(bs, be)) >> PPC_BITLSHIFT(be))
  85. #define AFUD_READ_INFO(afu) AFUD_READ(afu, 0x0)
  86. #define AFUD_NUM_INTS_PER_PROC(val) EXTRACT_PPC_BITS(val, 0, 15)
  87. #define AFUD_NUM_PROCS(val) EXTRACT_PPC_BITS(val, 16, 31)
  88. #define AFUD_NUM_CRS(val) EXTRACT_PPC_BITS(val, 32, 47)
  89. #define AFUD_MULTIMODE(val) EXTRACT_PPC_BIT(val, 48)
  90. #define AFUD_PUSH_BLOCK_TRANSFER(val) EXTRACT_PPC_BIT(val, 55)
  91. #define AFUD_DEDICATED_PROCESS(val) EXTRACT_PPC_BIT(val, 59)
  92. #define AFUD_AFU_DIRECTED(val) EXTRACT_PPC_BIT(val, 61)
  93. #define AFUD_TIME_SLICED(val) EXTRACT_PPC_BIT(val, 63)
  94. #define AFUD_READ_CR(afu) AFUD_READ(afu, 0x20)
  95. #define AFUD_CR_LEN(val) EXTRACT_PPC_BITS(val, 8, 63)
  96. #define AFUD_READ_CR_OFF(afu) AFUD_READ(afu, 0x28)
  97. #define AFUD_READ_PPPSA(afu) AFUD_READ(afu, 0x30)
  98. #define AFUD_PPPSA_PP(val) EXTRACT_PPC_BIT(val, 6)
  99. #define AFUD_PPPSA_PSA(val) EXTRACT_PPC_BIT(val, 7)
  100. #define AFUD_PPPSA_LEN(val) EXTRACT_PPC_BITS(val, 8, 63)
  101. #define AFUD_READ_PPPSA_OFF(afu) AFUD_READ(afu, 0x38)
  102. #define AFUD_READ_EB(afu) AFUD_READ(afu, 0x40)
  103. #define AFUD_EB_LEN(val) EXTRACT_PPC_BITS(val, 8, 63)
  104. #define AFUD_READ_EB_OFF(afu) AFUD_READ(afu, 0x48)
  105. static const struct pci_device_id cxl_pci_tbl[] = {
  106. { PCI_DEVICE(PCI_VENDOR_ID_IBM, 0x0477), },
  107. { PCI_DEVICE(PCI_VENDOR_ID_IBM, 0x044b), },
  108. { PCI_DEVICE(PCI_VENDOR_ID_IBM, 0x04cf), },
  109. { PCI_DEVICE(PCI_VENDOR_ID_IBM, 0x0601), },
  110. { PCI_DEVICE_CLASS(0x120000, ~0), },
  111. { }
  112. };
  113. MODULE_DEVICE_TABLE(pci, cxl_pci_tbl);
  114. /*
  115. * Mostly using these wrappers to avoid confusion:
  116. * priv 1 is BAR2, while priv 2 is BAR0
  117. */
  118. static inline resource_size_t p1_base(struct pci_dev *dev)
  119. {
  120. return pci_resource_start(dev, 2);
  121. }
  122. static inline resource_size_t p1_size(struct pci_dev *dev)
  123. {
  124. return pci_resource_len(dev, 2);
  125. }
  126. static inline resource_size_t p2_base(struct pci_dev *dev)
  127. {
  128. return pci_resource_start(dev, 0);
  129. }
  130. static inline resource_size_t p2_size(struct pci_dev *dev)
  131. {
  132. return pci_resource_len(dev, 0);
  133. }
  134. static int find_cxl_vsec(struct pci_dev *dev)
  135. {
  136. int vsec = 0;
  137. u16 val;
  138. while ((vsec = pci_find_next_ext_capability(dev, vsec, PCI_EXT_CAP_ID_VNDR))) {
  139. pci_read_config_word(dev, vsec + 0x4, &val);
  140. if (val == CXL_PCI_VSEC_ID)
  141. return vsec;
  142. }
  143. return 0;
  144. }
  145. static void dump_cxl_config_space(struct pci_dev *dev)
  146. {
  147. int vsec;
  148. u32 val;
  149. dev_info(&dev->dev, "dump_cxl_config_space\n");
  150. pci_read_config_dword(dev, PCI_BASE_ADDRESS_0, &val);
  151. dev_info(&dev->dev, "BAR0: %#.8x\n", val);
  152. pci_read_config_dword(dev, PCI_BASE_ADDRESS_1, &val);
  153. dev_info(&dev->dev, "BAR1: %#.8x\n", val);
  154. pci_read_config_dword(dev, PCI_BASE_ADDRESS_2, &val);
  155. dev_info(&dev->dev, "BAR2: %#.8x\n", val);
  156. pci_read_config_dword(dev, PCI_BASE_ADDRESS_3, &val);
  157. dev_info(&dev->dev, "BAR3: %#.8x\n", val);
  158. pci_read_config_dword(dev, PCI_BASE_ADDRESS_4, &val);
  159. dev_info(&dev->dev, "BAR4: %#.8x\n", val);
  160. pci_read_config_dword(dev, PCI_BASE_ADDRESS_5, &val);
  161. dev_info(&dev->dev, "BAR5: %#.8x\n", val);
  162. dev_info(&dev->dev, "p1 regs: %#llx, len: %#llx\n",
  163. p1_base(dev), p1_size(dev));
  164. dev_info(&dev->dev, "p2 regs: %#llx, len: %#llx\n",
  165. p2_base(dev), p2_size(dev));
  166. dev_info(&dev->dev, "BAR 4/5: %#llx, len: %#llx\n",
  167. pci_resource_start(dev, 4), pci_resource_len(dev, 4));
  168. if (!(vsec = find_cxl_vsec(dev)))
  169. return;
  170. #define show_reg(name, what) \
  171. dev_info(&dev->dev, "cxl vsec: %30s: %#x\n", name, what)
  172. pci_read_config_dword(dev, vsec + 0x0, &val);
  173. show_reg("Cap ID", (val >> 0) & 0xffff);
  174. show_reg("Cap Ver", (val >> 16) & 0xf);
  175. show_reg("Next Cap Ptr", (val >> 20) & 0xfff);
  176. pci_read_config_dword(dev, vsec + 0x4, &val);
  177. show_reg("VSEC ID", (val >> 0) & 0xffff);
  178. show_reg("VSEC Rev", (val >> 16) & 0xf);
  179. show_reg("VSEC Length", (val >> 20) & 0xfff);
  180. pci_read_config_dword(dev, vsec + 0x8, &val);
  181. show_reg("Num AFUs", (val >> 0) & 0xff);
  182. show_reg("Status", (val >> 8) & 0xff);
  183. show_reg("Mode Control", (val >> 16) & 0xff);
  184. show_reg("Reserved", (val >> 24) & 0xff);
  185. pci_read_config_dword(dev, vsec + 0xc, &val);
  186. show_reg("PSL Rev", (val >> 0) & 0xffff);
  187. show_reg("CAIA Ver", (val >> 16) & 0xffff);
  188. pci_read_config_dword(dev, vsec + 0x10, &val);
  189. show_reg("Base Image Rev", (val >> 0) & 0xffff);
  190. show_reg("Reserved", (val >> 16) & 0x0fff);
  191. show_reg("Image Control", (val >> 28) & 0x3);
  192. show_reg("Reserved", (val >> 30) & 0x1);
  193. show_reg("Image Loaded", (val >> 31) & 0x1);
  194. pci_read_config_dword(dev, vsec + 0x14, &val);
  195. show_reg("Reserved", val);
  196. pci_read_config_dword(dev, vsec + 0x18, &val);
  197. show_reg("Reserved", val);
  198. pci_read_config_dword(dev, vsec + 0x1c, &val);
  199. show_reg("Reserved", val);
  200. pci_read_config_dword(dev, vsec + 0x20, &val);
  201. show_reg("AFU Descriptor Offset", val);
  202. pci_read_config_dword(dev, vsec + 0x24, &val);
  203. show_reg("AFU Descriptor Size", val);
  204. pci_read_config_dword(dev, vsec + 0x28, &val);
  205. show_reg("Problem State Offset", val);
  206. pci_read_config_dword(dev, vsec + 0x2c, &val);
  207. show_reg("Problem State Size", val);
  208. pci_read_config_dword(dev, vsec + 0x30, &val);
  209. show_reg("Reserved", val);
  210. pci_read_config_dword(dev, vsec + 0x34, &val);
  211. show_reg("Reserved", val);
  212. pci_read_config_dword(dev, vsec + 0x38, &val);
  213. show_reg("Reserved", val);
  214. pci_read_config_dword(dev, vsec + 0x3c, &val);
  215. show_reg("Reserved", val);
  216. pci_read_config_dword(dev, vsec + 0x40, &val);
  217. show_reg("PSL Programming Port", val);
  218. pci_read_config_dword(dev, vsec + 0x44, &val);
  219. show_reg("PSL Programming Control", val);
  220. pci_read_config_dword(dev, vsec + 0x48, &val);
  221. show_reg("Reserved", val);
  222. pci_read_config_dword(dev, vsec + 0x4c, &val);
  223. show_reg("Reserved", val);
  224. pci_read_config_dword(dev, vsec + 0x50, &val);
  225. show_reg("Flash Address Register", val);
  226. pci_read_config_dword(dev, vsec + 0x54, &val);
  227. show_reg("Flash Size Register", val);
  228. pci_read_config_dword(dev, vsec + 0x58, &val);
  229. show_reg("Flash Status/Control Register", val);
  230. pci_read_config_dword(dev, vsec + 0x58, &val);
  231. show_reg("Flash Data Port", val);
  232. #undef show_reg
  233. }
  234. static void dump_afu_descriptor(struct cxl_afu *afu)
  235. {
  236. u64 val, afu_cr_num, afu_cr_off, afu_cr_len;
  237. int i;
  238. #define show_reg(name, what) \
  239. dev_info(&afu->dev, "afu desc: %30s: %#llx\n", name, what)
  240. val = AFUD_READ_INFO(afu);
  241. show_reg("num_ints_per_process", AFUD_NUM_INTS_PER_PROC(val));
  242. show_reg("num_of_processes", AFUD_NUM_PROCS(val));
  243. show_reg("num_of_afu_CRs", AFUD_NUM_CRS(val));
  244. show_reg("req_prog_mode", val & 0xffffULL);
  245. afu_cr_num = AFUD_NUM_CRS(val);
  246. val = AFUD_READ(afu, 0x8);
  247. show_reg("Reserved", val);
  248. val = AFUD_READ(afu, 0x10);
  249. show_reg("Reserved", val);
  250. val = AFUD_READ(afu, 0x18);
  251. show_reg("Reserved", val);
  252. val = AFUD_READ_CR(afu);
  253. show_reg("Reserved", (val >> (63-7)) & 0xff);
  254. show_reg("AFU_CR_len", AFUD_CR_LEN(val));
  255. afu_cr_len = AFUD_CR_LEN(val) * 256;
  256. val = AFUD_READ_CR_OFF(afu);
  257. afu_cr_off = val;
  258. show_reg("AFU_CR_offset", val);
  259. val = AFUD_READ_PPPSA(afu);
  260. show_reg("PerProcessPSA_control", (val >> (63-7)) & 0xff);
  261. show_reg("PerProcessPSA Length", AFUD_PPPSA_LEN(val));
  262. val = AFUD_READ_PPPSA_OFF(afu);
  263. show_reg("PerProcessPSA_offset", val);
  264. val = AFUD_READ_EB(afu);
  265. show_reg("Reserved", (val >> (63-7)) & 0xff);
  266. show_reg("AFU_EB_len", AFUD_EB_LEN(val));
  267. val = AFUD_READ_EB_OFF(afu);
  268. show_reg("AFU_EB_offset", val);
  269. for (i = 0; i < afu_cr_num; i++) {
  270. val = AFUD_READ_LE(afu, afu_cr_off + i * afu_cr_len);
  271. show_reg("CR Vendor", val & 0xffff);
  272. show_reg("CR Device", (val >> 16) & 0xffff);
  273. }
  274. #undef show_reg
  275. }
  276. static int init_implementation_adapter_regs(struct cxl *adapter, struct pci_dev *dev)
  277. {
  278. struct device_node *np;
  279. const __be32 *prop;
  280. u64 psl_dsnctl;
  281. u64 chipid;
  282. if (!(np = pnv_pci_get_phb_node(dev)))
  283. return -ENODEV;
  284. while (np && !(prop = of_get_property(np, "ibm,chip-id", NULL)))
  285. np = of_get_next_parent(np);
  286. if (!np)
  287. return -ENODEV;
  288. chipid = be32_to_cpup(prop);
  289. of_node_put(np);
  290. /* Tell PSL where to route data to */
  291. psl_dsnctl = 0x02E8900002000000ULL | (chipid << (63-5));
  292. cxl_p1_write(adapter, CXL_PSL_DSNDCTL, psl_dsnctl);
  293. cxl_p1_write(adapter, CXL_PSL_RESLCKTO, 0x20000000200ULL);
  294. /* snoop write mask */
  295. cxl_p1_write(adapter, CXL_PSL_SNWRALLOC, 0x00000000FFFFFFFFULL);
  296. /* set fir_accum */
  297. cxl_p1_write(adapter, CXL_PSL_FIR_CNTL, 0x0800000000000000ULL);
  298. /* for debugging with trace arrays */
  299. cxl_p1_write(adapter, CXL_PSL_TRACE, 0x0000FF7C00000000ULL);
  300. return 0;
  301. }
  302. #define TBSYNC_CNT(n) (((u64)n & 0x7) << (63-6))
  303. #define _2048_250MHZ_CYCLES 1
  304. static int cxl_setup_psl_timebase(struct cxl *adapter, struct pci_dev *dev)
  305. {
  306. u64 psl_tb;
  307. int delta;
  308. unsigned int retry = 0;
  309. struct device_node *np;
  310. if (!(np = pnv_pci_get_phb_node(dev)))
  311. return -ENODEV;
  312. /* Do not fail when CAPP timebase sync is not supported by OPAL */
  313. of_node_get(np);
  314. if (! of_get_property(np, "ibm,capp-timebase-sync", NULL)) {
  315. of_node_put(np);
  316. pr_err("PSL: Timebase sync: OPAL support missing\n");
  317. return 0;
  318. }
  319. of_node_put(np);
  320. /*
  321. * Setup PSL Timebase Control and Status register
  322. * with the recommended Timebase Sync Count value
  323. */
  324. cxl_p1_write(adapter, CXL_PSL_TB_CTLSTAT,
  325. TBSYNC_CNT(2 * _2048_250MHZ_CYCLES));
  326. /* Enable PSL Timebase */
  327. cxl_p1_write(adapter, CXL_PSL_Control, 0x0000000000000000);
  328. cxl_p1_write(adapter, CXL_PSL_Control, CXL_PSL_Control_tb);
  329. /* Wait until CORE TB and PSL TB difference <= 16usecs */
  330. do {
  331. msleep(1);
  332. if (retry++ > 5) {
  333. pr_err("PSL: Timebase sync: giving up!\n");
  334. return -EIO;
  335. }
  336. psl_tb = cxl_p1_read(adapter, CXL_PSL_Timebase);
  337. delta = mftb() - psl_tb;
  338. if (delta < 0)
  339. delta = -delta;
  340. } while (cputime_to_usecs(delta) > 16);
  341. return 0;
  342. }
  343. static int init_implementation_afu_regs(struct cxl_afu *afu)
  344. {
  345. /* read/write masks for this slice */
  346. cxl_p1n_write(afu, CXL_PSL_APCALLOC_A, 0xFFFFFFFEFEFEFEFEULL);
  347. /* APC read/write masks for this slice */
  348. cxl_p1n_write(afu, CXL_PSL_COALLOC_A, 0xFF000000FEFEFEFEULL);
  349. /* for debugging with trace arrays */
  350. cxl_p1n_write(afu, CXL_PSL_SLICE_TRACE, 0x0000FFFF00000000ULL);
  351. cxl_p1n_write(afu, CXL_PSL_RXCTL_A, CXL_PSL_RXCTL_AFUHP_4S);
  352. return 0;
  353. }
  354. int cxl_pci_setup_irq(struct cxl *adapter, unsigned int hwirq,
  355. unsigned int virq)
  356. {
  357. struct pci_dev *dev = to_pci_dev(adapter->dev.parent);
  358. return pnv_cxl_ioda_msi_setup(dev, hwirq, virq);
  359. }
  360. int cxl_update_image_control(struct cxl *adapter)
  361. {
  362. struct pci_dev *dev = to_pci_dev(adapter->dev.parent);
  363. int rc;
  364. int vsec;
  365. u8 image_state;
  366. if (!(vsec = find_cxl_vsec(dev))) {
  367. dev_err(&dev->dev, "ABORTING: CXL VSEC not found!\n");
  368. return -ENODEV;
  369. }
  370. if ((rc = CXL_READ_VSEC_IMAGE_STATE(dev, vsec, &image_state))) {
  371. dev_err(&dev->dev, "failed to read image state: %i\n", rc);
  372. return rc;
  373. }
  374. if (adapter->perst_loads_image)
  375. image_state |= CXL_VSEC_PERST_LOADS_IMAGE;
  376. else
  377. image_state &= ~CXL_VSEC_PERST_LOADS_IMAGE;
  378. if (adapter->perst_select_user)
  379. image_state |= CXL_VSEC_PERST_SELECT_USER;
  380. else
  381. image_state &= ~CXL_VSEC_PERST_SELECT_USER;
  382. if ((rc = CXL_WRITE_VSEC_IMAGE_STATE(dev, vsec, image_state))) {
  383. dev_err(&dev->dev, "failed to update image control: %i\n", rc);
  384. return rc;
  385. }
  386. return 0;
  387. }
  388. int cxl_pci_alloc_one_irq(struct cxl *adapter)
  389. {
  390. struct pci_dev *dev = to_pci_dev(adapter->dev.parent);
  391. return pnv_cxl_alloc_hwirqs(dev, 1);
  392. }
  393. void cxl_pci_release_one_irq(struct cxl *adapter, int hwirq)
  394. {
  395. struct pci_dev *dev = to_pci_dev(adapter->dev.parent);
  396. return pnv_cxl_release_hwirqs(dev, hwirq, 1);
  397. }
  398. int cxl_pci_alloc_irq_ranges(struct cxl_irq_ranges *irqs,
  399. struct cxl *adapter, unsigned int num)
  400. {
  401. struct pci_dev *dev = to_pci_dev(adapter->dev.parent);
  402. return pnv_cxl_alloc_hwirq_ranges(irqs, dev, num);
  403. }
  404. void cxl_pci_release_irq_ranges(struct cxl_irq_ranges *irqs,
  405. struct cxl *adapter)
  406. {
  407. struct pci_dev *dev = to_pci_dev(adapter->dev.parent);
  408. pnv_cxl_release_hwirq_ranges(irqs, dev);
  409. }
  410. static int setup_cxl_bars(struct pci_dev *dev)
  411. {
  412. /* Safety check in case we get backported to < 3.17 without M64 */
  413. if ((p1_base(dev) < 0x100000000ULL) ||
  414. (p2_base(dev) < 0x100000000ULL)) {
  415. dev_err(&dev->dev, "ABORTING: M32 BAR assignment incompatible with CXL\n");
  416. return -ENODEV;
  417. }
  418. /*
  419. * BAR 4/5 has a special meaning for CXL and must be programmed with a
  420. * special value corresponding to the CXL protocol address range.
  421. * For POWER 8 that means bits 48:49 must be set to 10
  422. */
  423. pci_write_config_dword(dev, PCI_BASE_ADDRESS_4, 0x00000000);
  424. pci_write_config_dword(dev, PCI_BASE_ADDRESS_5, 0x00020000);
  425. return 0;
  426. }
  427. /* pciex node: ibm,opal-m64-window = <0x3d058 0x0 0x3d058 0x0 0x8 0x0>; */
  428. static int switch_card_to_cxl(struct pci_dev *dev)
  429. {
  430. int vsec;
  431. u8 val;
  432. int rc;
  433. dev_info(&dev->dev, "switch card to CXL\n");
  434. if (!(vsec = find_cxl_vsec(dev))) {
  435. dev_err(&dev->dev, "ABORTING: CXL VSEC not found!\n");
  436. return -ENODEV;
  437. }
  438. if ((rc = CXL_READ_VSEC_MODE_CONTROL(dev, vsec, &val))) {
  439. dev_err(&dev->dev, "failed to read current mode control: %i", rc);
  440. return rc;
  441. }
  442. val &= ~CXL_VSEC_PROTOCOL_MASK;
  443. val |= CXL_VSEC_PROTOCOL_256TB | CXL_VSEC_PROTOCOL_ENABLE;
  444. if ((rc = CXL_WRITE_VSEC_MODE_CONTROL(dev, vsec, val))) {
  445. dev_err(&dev->dev, "failed to enable CXL protocol: %i", rc);
  446. return rc;
  447. }
  448. /*
  449. * The CAIA spec (v0.12 11.6 Bi-modal Device Support) states
  450. * we must wait 100ms after this mode switch before touching
  451. * PCIe config space.
  452. */
  453. msleep(100);
  454. return 0;
  455. }
  456. static int pci_map_slice_regs(struct cxl_afu *afu, struct cxl *adapter, struct pci_dev *dev)
  457. {
  458. u64 p1n_base, p2n_base, afu_desc;
  459. const u64 p1n_size = 0x100;
  460. const u64 p2n_size = 0x1000;
  461. p1n_base = p1_base(dev) + 0x10000 + (afu->slice * p1n_size);
  462. p2n_base = p2_base(dev) + (afu->slice * p2n_size);
  463. afu->psn_phys = p2_base(dev) + (adapter->native->ps_off + (afu->slice * adapter->ps_size));
  464. afu_desc = p2_base(dev) + adapter->native->afu_desc_off + (afu->slice * adapter->native->afu_desc_size);
  465. if (!(afu->native->p1n_mmio = ioremap(p1n_base, p1n_size)))
  466. goto err;
  467. if (!(afu->p2n_mmio = ioremap(p2n_base, p2n_size)))
  468. goto err1;
  469. if (afu_desc) {
  470. if (!(afu->native->afu_desc_mmio = ioremap(afu_desc, adapter->native->afu_desc_size)))
  471. goto err2;
  472. }
  473. return 0;
  474. err2:
  475. iounmap(afu->p2n_mmio);
  476. err1:
  477. iounmap(afu->native->p1n_mmio);
  478. err:
  479. dev_err(&afu->dev, "Error mapping AFU MMIO regions\n");
  480. return -ENOMEM;
  481. }
  482. static void pci_unmap_slice_regs(struct cxl_afu *afu)
  483. {
  484. if (afu->p2n_mmio) {
  485. iounmap(afu->p2n_mmio);
  486. afu->p2n_mmio = NULL;
  487. }
  488. if (afu->native->p1n_mmio) {
  489. iounmap(afu->native->p1n_mmio);
  490. afu->native->p1n_mmio = NULL;
  491. }
  492. if (afu->native->afu_desc_mmio) {
  493. iounmap(afu->native->afu_desc_mmio);
  494. afu->native->afu_desc_mmio = NULL;
  495. }
  496. }
  497. void cxl_pci_release_afu(struct device *dev)
  498. {
  499. struct cxl_afu *afu = to_cxl_afu(dev);
  500. pr_devel("%s\n", __func__);
  501. idr_destroy(&afu->contexts_idr);
  502. cxl_release_spa(afu);
  503. kfree(afu->native);
  504. kfree(afu);
  505. }
  506. /* Expects AFU struct to have recently been zeroed out */
  507. static int cxl_read_afu_descriptor(struct cxl_afu *afu)
  508. {
  509. u64 val;
  510. val = AFUD_READ_INFO(afu);
  511. afu->pp_irqs = AFUD_NUM_INTS_PER_PROC(val);
  512. afu->max_procs_virtualised = AFUD_NUM_PROCS(val);
  513. afu->crs_num = AFUD_NUM_CRS(val);
  514. if (AFUD_AFU_DIRECTED(val))
  515. afu->modes_supported |= CXL_MODE_DIRECTED;
  516. if (AFUD_DEDICATED_PROCESS(val))
  517. afu->modes_supported |= CXL_MODE_DEDICATED;
  518. if (AFUD_TIME_SLICED(val))
  519. afu->modes_supported |= CXL_MODE_TIME_SLICED;
  520. val = AFUD_READ_PPPSA(afu);
  521. afu->pp_size = AFUD_PPPSA_LEN(val) * 4096;
  522. afu->psa = AFUD_PPPSA_PSA(val);
  523. if ((afu->pp_psa = AFUD_PPPSA_PP(val)))
  524. afu->native->pp_offset = AFUD_READ_PPPSA_OFF(afu);
  525. val = AFUD_READ_CR(afu);
  526. afu->crs_len = AFUD_CR_LEN(val) * 256;
  527. afu->crs_offset = AFUD_READ_CR_OFF(afu);
  528. /* eb_len is in multiple of 4K */
  529. afu->eb_len = AFUD_EB_LEN(AFUD_READ_EB(afu)) * 4096;
  530. afu->eb_offset = AFUD_READ_EB_OFF(afu);
  531. /* eb_off is 4K aligned so lower 12 bits are always zero */
  532. if (EXTRACT_PPC_BITS(afu->eb_offset, 0, 11) != 0) {
  533. dev_warn(&afu->dev,
  534. "Invalid AFU error buffer offset %Lx\n",
  535. afu->eb_offset);
  536. dev_info(&afu->dev,
  537. "Ignoring AFU error buffer in the descriptor\n");
  538. /* indicate that no afu buffer exists */
  539. afu->eb_len = 0;
  540. }
  541. return 0;
  542. }
  543. static int cxl_afu_descriptor_looks_ok(struct cxl_afu *afu)
  544. {
  545. int i, rc;
  546. u32 val;
  547. if (afu->psa && afu->adapter->ps_size <
  548. (afu->native->pp_offset + afu->pp_size*afu->max_procs_virtualised)) {
  549. dev_err(&afu->dev, "per-process PSA can't fit inside the PSA!\n");
  550. return -ENODEV;
  551. }
  552. if (afu->pp_psa && (afu->pp_size < PAGE_SIZE))
  553. dev_warn(&afu->dev, "AFU uses < PAGE_SIZE per-process PSA!");
  554. for (i = 0; i < afu->crs_num; i++) {
  555. rc = cxl_ops->afu_cr_read32(afu, i, 0, &val);
  556. if (rc || val == 0) {
  557. dev_err(&afu->dev, "ABORTING: AFU configuration record %i is invalid\n", i);
  558. return -EINVAL;
  559. }
  560. }
  561. return 0;
  562. }
  563. static int sanitise_afu_regs(struct cxl_afu *afu)
  564. {
  565. u64 reg;
  566. /*
  567. * Clear out any regs that contain either an IVTE or address or may be
  568. * waiting on an acknowledgement to try to be a bit safer as we bring
  569. * it online
  570. */
  571. reg = cxl_p2n_read(afu, CXL_AFU_Cntl_An);
  572. if ((reg & CXL_AFU_Cntl_An_ES_MASK) != CXL_AFU_Cntl_An_ES_Disabled) {
  573. dev_warn(&afu->dev, "WARNING: AFU was not disabled: %#016llx\n", reg);
  574. if (cxl_ops->afu_reset(afu))
  575. return -EIO;
  576. if (cxl_afu_disable(afu))
  577. return -EIO;
  578. if (cxl_psl_purge(afu))
  579. return -EIO;
  580. }
  581. cxl_p1n_write(afu, CXL_PSL_SPAP_An, 0x0000000000000000);
  582. cxl_p1n_write(afu, CXL_PSL_IVTE_Limit_An, 0x0000000000000000);
  583. cxl_p1n_write(afu, CXL_PSL_IVTE_Offset_An, 0x0000000000000000);
  584. cxl_p1n_write(afu, CXL_PSL_AMBAR_An, 0x0000000000000000);
  585. cxl_p1n_write(afu, CXL_PSL_SPOffset_An, 0x0000000000000000);
  586. cxl_p1n_write(afu, CXL_HAURP_An, 0x0000000000000000);
  587. cxl_p2n_write(afu, CXL_CSRP_An, 0x0000000000000000);
  588. cxl_p2n_write(afu, CXL_AURP1_An, 0x0000000000000000);
  589. cxl_p2n_write(afu, CXL_AURP0_An, 0x0000000000000000);
  590. cxl_p2n_write(afu, CXL_SSTP1_An, 0x0000000000000000);
  591. cxl_p2n_write(afu, CXL_SSTP0_An, 0x0000000000000000);
  592. reg = cxl_p2n_read(afu, CXL_PSL_DSISR_An);
  593. if (reg) {
  594. dev_warn(&afu->dev, "AFU had pending DSISR: %#016llx\n", reg);
  595. if (reg & CXL_PSL_DSISR_TRANS)
  596. cxl_p2n_write(afu, CXL_PSL_TFC_An, CXL_PSL_TFC_An_AE);
  597. else
  598. cxl_p2n_write(afu, CXL_PSL_TFC_An, CXL_PSL_TFC_An_A);
  599. }
  600. reg = cxl_p1n_read(afu, CXL_PSL_SERR_An);
  601. if (reg) {
  602. if (reg & ~0xffff)
  603. dev_warn(&afu->dev, "AFU had pending SERR: %#016llx\n", reg);
  604. cxl_p1n_write(afu, CXL_PSL_SERR_An, reg & ~0xffff);
  605. }
  606. reg = cxl_p2n_read(afu, CXL_PSL_ErrStat_An);
  607. if (reg) {
  608. dev_warn(&afu->dev, "AFU had pending error status: %#016llx\n", reg);
  609. cxl_p2n_write(afu, CXL_PSL_ErrStat_An, reg);
  610. }
  611. return 0;
  612. }
  613. #define ERR_BUFF_MAX_COPY_SIZE PAGE_SIZE
  614. /*
  615. * afu_eb_read:
  616. * Called from sysfs and reads the afu error info buffer. The h/w only supports
  617. * 4/8 bytes aligned access. So in case the requested offset/count arent 8 byte
  618. * aligned the function uses a bounce buffer which can be max PAGE_SIZE.
  619. */
  620. ssize_t cxl_pci_afu_read_err_buffer(struct cxl_afu *afu, char *buf,
  621. loff_t off, size_t count)
  622. {
  623. loff_t aligned_start, aligned_end;
  624. size_t aligned_length;
  625. void *tbuf;
  626. const void __iomem *ebuf = afu->native->afu_desc_mmio + afu->eb_offset;
  627. if (count == 0 || off < 0 || (size_t)off >= afu->eb_len)
  628. return 0;
  629. /* calculate aligned read window */
  630. count = min((size_t)(afu->eb_len - off), count);
  631. aligned_start = round_down(off, 8);
  632. aligned_end = round_up(off + count, 8);
  633. aligned_length = aligned_end - aligned_start;
  634. /* max we can copy in one read is PAGE_SIZE */
  635. if (aligned_length > ERR_BUFF_MAX_COPY_SIZE) {
  636. aligned_length = ERR_BUFF_MAX_COPY_SIZE;
  637. count = ERR_BUFF_MAX_COPY_SIZE - (off & 0x7);
  638. }
  639. /* use bounce buffer for copy */
  640. tbuf = (void *)__get_free_page(GFP_TEMPORARY);
  641. if (!tbuf)
  642. return -ENOMEM;
  643. /* perform aligned read from the mmio region */
  644. memcpy_fromio(tbuf, ebuf + aligned_start, aligned_length);
  645. memcpy(buf, tbuf + (off & 0x7), count);
  646. free_page((unsigned long)tbuf);
  647. return count;
  648. }
  649. static int pci_configure_afu(struct cxl_afu *afu, struct cxl *adapter, struct pci_dev *dev)
  650. {
  651. int rc;
  652. if ((rc = pci_map_slice_regs(afu, adapter, dev)))
  653. return rc;
  654. if ((rc = sanitise_afu_regs(afu)))
  655. goto err1;
  656. /* We need to reset the AFU before we can read the AFU descriptor */
  657. if ((rc = cxl_ops->afu_reset(afu)))
  658. goto err1;
  659. if (cxl_verbose)
  660. dump_afu_descriptor(afu);
  661. if ((rc = cxl_read_afu_descriptor(afu)))
  662. goto err1;
  663. if ((rc = cxl_afu_descriptor_looks_ok(afu)))
  664. goto err1;
  665. if ((rc = init_implementation_afu_regs(afu)))
  666. goto err1;
  667. if ((rc = cxl_native_register_serr_irq(afu)))
  668. goto err1;
  669. if ((rc = cxl_native_register_psl_irq(afu)))
  670. goto err2;
  671. return 0;
  672. err2:
  673. cxl_native_release_serr_irq(afu);
  674. err1:
  675. pci_unmap_slice_regs(afu);
  676. return rc;
  677. }
  678. static void pci_deconfigure_afu(struct cxl_afu *afu)
  679. {
  680. cxl_native_release_psl_irq(afu);
  681. cxl_native_release_serr_irq(afu);
  682. pci_unmap_slice_regs(afu);
  683. }
  684. static int pci_init_afu(struct cxl *adapter, int slice, struct pci_dev *dev)
  685. {
  686. struct cxl_afu *afu;
  687. int rc = -ENOMEM;
  688. afu = cxl_alloc_afu(adapter, slice);
  689. if (!afu)
  690. return -ENOMEM;
  691. afu->native = kzalloc(sizeof(struct cxl_afu_native), GFP_KERNEL);
  692. if (!afu->native)
  693. goto err_free_afu;
  694. mutex_init(&afu->native->spa_mutex);
  695. rc = dev_set_name(&afu->dev, "afu%i.%i", adapter->adapter_num, slice);
  696. if (rc)
  697. goto err_free_native;
  698. rc = pci_configure_afu(afu, adapter, dev);
  699. if (rc)
  700. goto err_free_native;
  701. /* Don't care if this fails */
  702. cxl_debugfs_afu_add(afu);
  703. /*
  704. * After we call this function we must not free the afu directly, even
  705. * if it returns an error!
  706. */
  707. if ((rc = cxl_register_afu(afu)))
  708. goto err_put1;
  709. if ((rc = cxl_sysfs_afu_add(afu)))
  710. goto err_put1;
  711. adapter->afu[afu->slice] = afu;
  712. if ((rc = cxl_pci_vphb_add(afu)))
  713. dev_info(&afu->dev, "Can't register vPHB\n");
  714. return 0;
  715. err_put1:
  716. pci_deconfigure_afu(afu);
  717. cxl_debugfs_afu_remove(afu);
  718. device_unregister(&afu->dev);
  719. return rc;
  720. err_free_native:
  721. kfree(afu->native);
  722. err_free_afu:
  723. kfree(afu);
  724. return rc;
  725. }
  726. static void cxl_pci_remove_afu(struct cxl_afu *afu)
  727. {
  728. pr_devel("%s\n", __func__);
  729. if (!afu)
  730. return;
  731. cxl_pci_vphb_remove(afu);
  732. cxl_sysfs_afu_remove(afu);
  733. cxl_debugfs_afu_remove(afu);
  734. spin_lock(&afu->adapter->afu_list_lock);
  735. afu->adapter->afu[afu->slice] = NULL;
  736. spin_unlock(&afu->adapter->afu_list_lock);
  737. cxl_context_detach_all(afu);
  738. cxl_ops->afu_deactivate_mode(afu, afu->current_mode);
  739. pci_deconfigure_afu(afu);
  740. device_unregister(&afu->dev);
  741. }
  742. int cxl_pci_reset(struct cxl *adapter)
  743. {
  744. struct pci_dev *dev = to_pci_dev(adapter->dev.parent);
  745. int rc;
  746. if (adapter->perst_same_image) {
  747. dev_warn(&dev->dev,
  748. "cxl: refusing to reset/reflash when perst_reloads_same_image is set.\n");
  749. return -EINVAL;
  750. }
  751. dev_info(&dev->dev, "CXL reset\n");
  752. /* pcie_warm_reset requests a fundamental pci reset which includes a
  753. * PERST assert/deassert. PERST triggers a loading of the image
  754. * if "user" or "factory" is selected in sysfs */
  755. if ((rc = pci_set_pcie_reset_state(dev, pcie_warm_reset))) {
  756. dev_err(&dev->dev, "cxl: pcie_warm_reset failed\n");
  757. return rc;
  758. }
  759. return rc;
  760. }
  761. static int cxl_map_adapter_regs(struct cxl *adapter, struct pci_dev *dev)
  762. {
  763. if (pci_request_region(dev, 2, "priv 2 regs"))
  764. goto err1;
  765. if (pci_request_region(dev, 0, "priv 1 regs"))
  766. goto err2;
  767. pr_devel("cxl_map_adapter_regs: p1: %#016llx %#llx, p2: %#016llx %#llx",
  768. p1_base(dev), p1_size(dev), p2_base(dev), p2_size(dev));
  769. if (!(adapter->native->p1_mmio = ioremap(p1_base(dev), p1_size(dev))))
  770. goto err3;
  771. if (!(adapter->native->p2_mmio = ioremap(p2_base(dev), p2_size(dev))))
  772. goto err4;
  773. return 0;
  774. err4:
  775. iounmap(adapter->native->p1_mmio);
  776. adapter->native->p1_mmio = NULL;
  777. err3:
  778. pci_release_region(dev, 0);
  779. err2:
  780. pci_release_region(dev, 2);
  781. err1:
  782. return -ENOMEM;
  783. }
  784. static void cxl_unmap_adapter_regs(struct cxl *adapter)
  785. {
  786. if (adapter->native->p1_mmio) {
  787. iounmap(adapter->native->p1_mmio);
  788. adapter->native->p1_mmio = NULL;
  789. pci_release_region(to_pci_dev(adapter->dev.parent), 2);
  790. }
  791. if (adapter->native->p2_mmio) {
  792. iounmap(adapter->native->p2_mmio);
  793. adapter->native->p2_mmio = NULL;
  794. pci_release_region(to_pci_dev(adapter->dev.parent), 0);
  795. }
  796. }
  797. static int cxl_read_vsec(struct cxl *adapter, struct pci_dev *dev)
  798. {
  799. int vsec;
  800. u32 afu_desc_off, afu_desc_size;
  801. u32 ps_off, ps_size;
  802. u16 vseclen;
  803. u8 image_state;
  804. if (!(vsec = find_cxl_vsec(dev))) {
  805. dev_err(&dev->dev, "ABORTING: CXL VSEC not found!\n");
  806. return -ENODEV;
  807. }
  808. CXL_READ_VSEC_LENGTH(dev, vsec, &vseclen);
  809. if (vseclen < CXL_VSEC_MIN_SIZE) {
  810. dev_err(&dev->dev, "ABORTING: CXL VSEC too short\n");
  811. return -EINVAL;
  812. }
  813. CXL_READ_VSEC_STATUS(dev, vsec, &adapter->vsec_status);
  814. CXL_READ_VSEC_PSL_REVISION(dev, vsec, &adapter->psl_rev);
  815. CXL_READ_VSEC_CAIA_MAJOR(dev, vsec, &adapter->caia_major);
  816. CXL_READ_VSEC_CAIA_MINOR(dev, vsec, &adapter->caia_minor);
  817. CXL_READ_VSEC_BASE_IMAGE(dev, vsec, &adapter->base_image);
  818. CXL_READ_VSEC_IMAGE_STATE(dev, vsec, &image_state);
  819. adapter->user_image_loaded = !!(image_state & CXL_VSEC_USER_IMAGE_LOADED);
  820. adapter->perst_select_user = !!(image_state & CXL_VSEC_USER_IMAGE_LOADED);
  821. CXL_READ_VSEC_NAFUS(dev, vsec, &adapter->slices);
  822. CXL_READ_VSEC_AFU_DESC_OFF(dev, vsec, &afu_desc_off);
  823. CXL_READ_VSEC_AFU_DESC_SIZE(dev, vsec, &afu_desc_size);
  824. CXL_READ_VSEC_PS_OFF(dev, vsec, &ps_off);
  825. CXL_READ_VSEC_PS_SIZE(dev, vsec, &ps_size);
  826. /* Convert everything to bytes, because there is NO WAY I'd look at the
  827. * code a month later and forget what units these are in ;-) */
  828. adapter->native->ps_off = ps_off * 64 * 1024;
  829. adapter->ps_size = ps_size * 64 * 1024;
  830. adapter->native->afu_desc_off = afu_desc_off * 64 * 1024;
  831. adapter->native->afu_desc_size = afu_desc_size * 64 * 1024;
  832. /* Total IRQs - 1 PSL ERROR - #AFU*(1 slice error + 1 DSI) */
  833. adapter->user_irqs = pnv_cxl_get_irq_count(dev) - 1 - 2*adapter->slices;
  834. return 0;
  835. }
  836. /*
  837. * Workaround a PCIe Host Bridge defect on some cards, that can cause
  838. * malformed Transaction Layer Packet (TLP) errors to be erroneously
  839. * reported. Mask this error in the Uncorrectable Error Mask Register.
  840. *
  841. * The upper nibble of the PSL revision is used to distinguish between
  842. * different cards. The affected ones have it set to 0.
  843. */
  844. static void cxl_fixup_malformed_tlp(struct cxl *adapter, struct pci_dev *dev)
  845. {
  846. int aer;
  847. u32 data;
  848. if (adapter->psl_rev & 0xf000)
  849. return;
  850. if (!(aer = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ERR)))
  851. return;
  852. pci_read_config_dword(dev, aer + PCI_ERR_UNCOR_MASK, &data);
  853. if (data & PCI_ERR_UNC_MALF_TLP)
  854. if (data & PCI_ERR_UNC_INTN)
  855. return;
  856. data |= PCI_ERR_UNC_MALF_TLP;
  857. data |= PCI_ERR_UNC_INTN;
  858. pci_write_config_dword(dev, aer + PCI_ERR_UNCOR_MASK, data);
  859. }
  860. static int cxl_vsec_looks_ok(struct cxl *adapter, struct pci_dev *dev)
  861. {
  862. if (adapter->vsec_status & CXL_STATUS_SECOND_PORT)
  863. return -EBUSY;
  864. if (adapter->vsec_status & CXL_UNSUPPORTED_FEATURES) {
  865. dev_err(&dev->dev, "ABORTING: CXL requires unsupported features\n");
  866. return -EINVAL;
  867. }
  868. if (!adapter->slices) {
  869. /* Once we support dynamic reprogramming we can use the card if
  870. * it supports loadable AFUs */
  871. dev_err(&dev->dev, "ABORTING: Device has no AFUs\n");
  872. return -EINVAL;
  873. }
  874. if (!adapter->native->afu_desc_off || !adapter->native->afu_desc_size) {
  875. dev_err(&dev->dev, "ABORTING: VSEC shows no AFU descriptors\n");
  876. return -EINVAL;
  877. }
  878. if (adapter->ps_size > p2_size(dev) - adapter->native->ps_off) {
  879. dev_err(&dev->dev, "ABORTING: Problem state size larger than "
  880. "available in BAR2: 0x%llx > 0x%llx\n",
  881. adapter->ps_size, p2_size(dev) - adapter->native->ps_off);
  882. return -EINVAL;
  883. }
  884. return 0;
  885. }
  886. ssize_t cxl_pci_read_adapter_vpd(struct cxl *adapter, void *buf, size_t len)
  887. {
  888. return pci_read_vpd(to_pci_dev(adapter->dev.parent), 0, len, buf);
  889. }
  890. static void cxl_release_adapter(struct device *dev)
  891. {
  892. struct cxl *adapter = to_cxl_adapter(dev);
  893. pr_devel("cxl_release_adapter\n");
  894. cxl_remove_adapter_nr(adapter);
  895. kfree(adapter->native);
  896. kfree(adapter);
  897. }
  898. #define CXL_PSL_ErrIVTE_tberror (0x1ull << (63-31))
  899. static int sanitise_adapter_regs(struct cxl *adapter)
  900. {
  901. /* Clear PSL tberror bit by writing 1 to it */
  902. cxl_p1_write(adapter, CXL_PSL_ErrIVTE, CXL_PSL_ErrIVTE_tberror);
  903. return cxl_tlb_slb_invalidate(adapter);
  904. }
  905. /* This should contain *only* operations that can safely be done in
  906. * both creation and recovery.
  907. */
  908. static int cxl_configure_adapter(struct cxl *adapter, struct pci_dev *dev)
  909. {
  910. int rc;
  911. adapter->dev.parent = &dev->dev;
  912. adapter->dev.release = cxl_release_adapter;
  913. pci_set_drvdata(dev, adapter);
  914. rc = pci_enable_device(dev);
  915. if (rc) {
  916. dev_err(&dev->dev, "pci_enable_device failed: %i\n", rc);
  917. return rc;
  918. }
  919. if ((rc = cxl_read_vsec(adapter, dev)))
  920. return rc;
  921. if ((rc = cxl_vsec_looks_ok(adapter, dev)))
  922. return rc;
  923. cxl_fixup_malformed_tlp(adapter, dev);
  924. if ((rc = setup_cxl_bars(dev)))
  925. return rc;
  926. if ((rc = switch_card_to_cxl(dev)))
  927. return rc;
  928. if ((rc = cxl_update_image_control(adapter)))
  929. return rc;
  930. if ((rc = cxl_map_adapter_regs(adapter, dev)))
  931. return rc;
  932. if ((rc = sanitise_adapter_regs(adapter)))
  933. goto err;
  934. if ((rc = init_implementation_adapter_regs(adapter, dev)))
  935. goto err;
  936. if ((rc = pnv_phb_to_cxl_mode(dev, OPAL_PHB_CAPI_MODE_CAPI)))
  937. goto err;
  938. /* If recovery happened, the last step is to turn on snooping.
  939. * In the non-recovery case this has no effect */
  940. if ((rc = pnv_phb_to_cxl_mode(dev, OPAL_PHB_CAPI_MODE_SNOOP_ON)))
  941. goto err;
  942. if ((rc = cxl_setup_psl_timebase(adapter, dev)))
  943. goto err;
  944. if ((rc = cxl_native_register_psl_err_irq(adapter)))
  945. goto err;
  946. return 0;
  947. err:
  948. cxl_unmap_adapter_regs(adapter);
  949. return rc;
  950. }
  951. static void cxl_deconfigure_adapter(struct cxl *adapter)
  952. {
  953. struct pci_dev *pdev = to_pci_dev(adapter->dev.parent);
  954. cxl_native_release_psl_err_irq(adapter);
  955. cxl_unmap_adapter_regs(adapter);
  956. pci_disable_device(pdev);
  957. }
  958. static struct cxl *cxl_pci_init_adapter(struct pci_dev *dev)
  959. {
  960. struct cxl *adapter;
  961. int rc;
  962. adapter = cxl_alloc_adapter();
  963. if (!adapter)
  964. return ERR_PTR(-ENOMEM);
  965. adapter->native = kzalloc(sizeof(struct cxl_native), GFP_KERNEL);
  966. if (!adapter->native) {
  967. rc = -ENOMEM;
  968. goto err_release;
  969. }
  970. /* Set defaults for parameters which need to persist over
  971. * configure/reconfigure
  972. */
  973. adapter->perst_loads_image = true;
  974. adapter->perst_same_image = false;
  975. rc = cxl_configure_adapter(adapter, dev);
  976. if (rc) {
  977. pci_disable_device(dev);
  978. goto err_release;
  979. }
  980. /* Don't care if this one fails: */
  981. cxl_debugfs_adapter_add(adapter);
  982. /*
  983. * After we call this function we must not free the adapter directly,
  984. * even if it returns an error!
  985. */
  986. if ((rc = cxl_register_adapter(adapter)))
  987. goto err_put1;
  988. if ((rc = cxl_sysfs_adapter_add(adapter)))
  989. goto err_put1;
  990. return adapter;
  991. err_put1:
  992. /* This should mirror cxl_remove_adapter, except without the
  993. * sysfs parts
  994. */
  995. cxl_debugfs_adapter_remove(adapter);
  996. cxl_deconfigure_adapter(adapter);
  997. device_unregister(&adapter->dev);
  998. return ERR_PTR(rc);
  999. err_release:
  1000. cxl_release_adapter(&adapter->dev);
  1001. return ERR_PTR(rc);
  1002. }
  1003. static void cxl_pci_remove_adapter(struct cxl *adapter)
  1004. {
  1005. pr_devel("cxl_remove_adapter\n");
  1006. cxl_sysfs_adapter_remove(adapter);
  1007. cxl_debugfs_adapter_remove(adapter);
  1008. cxl_deconfigure_adapter(adapter);
  1009. device_unregister(&adapter->dev);
  1010. }
  1011. static int cxl_probe(struct pci_dev *dev, const struct pci_device_id *id)
  1012. {
  1013. struct cxl *adapter;
  1014. int slice;
  1015. int rc;
  1016. if (cxl_pci_is_vphb_device(dev)) {
  1017. dev_dbg(&dev->dev, "cxl_init_adapter: Ignoring cxl vphb device\n");
  1018. return -ENODEV;
  1019. }
  1020. if (cxl_verbose)
  1021. dump_cxl_config_space(dev);
  1022. adapter = cxl_pci_init_adapter(dev);
  1023. if (IS_ERR(adapter)) {
  1024. dev_err(&dev->dev, "cxl_init_adapter failed: %li\n", PTR_ERR(adapter));
  1025. return PTR_ERR(adapter);
  1026. }
  1027. for (slice = 0; slice < adapter->slices; slice++) {
  1028. if ((rc = pci_init_afu(adapter, slice, dev))) {
  1029. dev_err(&dev->dev, "AFU %i failed to initialise: %i\n", slice, rc);
  1030. continue;
  1031. }
  1032. rc = cxl_afu_select_best_mode(adapter->afu[slice]);
  1033. if (rc)
  1034. dev_err(&dev->dev, "AFU %i failed to start: %i\n", slice, rc);
  1035. }
  1036. return 0;
  1037. }
  1038. static void cxl_remove(struct pci_dev *dev)
  1039. {
  1040. struct cxl *adapter = pci_get_drvdata(dev);
  1041. struct cxl_afu *afu;
  1042. int i;
  1043. /*
  1044. * Lock to prevent someone grabbing a ref through the adapter list as
  1045. * we are removing it
  1046. */
  1047. for (i = 0; i < adapter->slices; i++) {
  1048. afu = adapter->afu[i];
  1049. cxl_pci_remove_afu(afu);
  1050. }
  1051. cxl_pci_remove_adapter(adapter);
  1052. }
  1053. static pci_ers_result_t cxl_vphb_error_detected(struct cxl_afu *afu,
  1054. pci_channel_state_t state)
  1055. {
  1056. struct pci_dev *afu_dev;
  1057. pci_ers_result_t result = PCI_ERS_RESULT_NEED_RESET;
  1058. pci_ers_result_t afu_result = PCI_ERS_RESULT_NEED_RESET;
  1059. /* There should only be one entry, but go through the list
  1060. * anyway
  1061. */
  1062. list_for_each_entry(afu_dev, &afu->phb->bus->devices, bus_list) {
  1063. if (!afu_dev->driver)
  1064. continue;
  1065. afu_dev->error_state = state;
  1066. if (afu_dev->driver->err_handler)
  1067. afu_result = afu_dev->driver->err_handler->error_detected(afu_dev,
  1068. state);
  1069. /* Disconnect trumps all, NONE trumps NEED_RESET */
  1070. if (afu_result == PCI_ERS_RESULT_DISCONNECT)
  1071. result = PCI_ERS_RESULT_DISCONNECT;
  1072. else if ((afu_result == PCI_ERS_RESULT_NONE) &&
  1073. (result == PCI_ERS_RESULT_NEED_RESET))
  1074. result = PCI_ERS_RESULT_NONE;
  1075. }
  1076. return result;
  1077. }
  1078. static pci_ers_result_t cxl_pci_error_detected(struct pci_dev *pdev,
  1079. pci_channel_state_t state)
  1080. {
  1081. struct cxl *adapter = pci_get_drvdata(pdev);
  1082. struct cxl_afu *afu;
  1083. pci_ers_result_t result = PCI_ERS_RESULT_NEED_RESET;
  1084. int i;
  1085. /* At this point, we could still have an interrupt pending.
  1086. * Let's try to get them out of the way before they do
  1087. * anything we don't like.
  1088. */
  1089. schedule();
  1090. /* If we're permanently dead, give up. */
  1091. if (state == pci_channel_io_perm_failure) {
  1092. /* Tell the AFU drivers; but we don't care what they
  1093. * say, we're going away.
  1094. */
  1095. for (i = 0; i < adapter->slices; i++) {
  1096. afu = adapter->afu[i];
  1097. cxl_vphb_error_detected(afu, state);
  1098. }
  1099. return PCI_ERS_RESULT_DISCONNECT;
  1100. }
  1101. /* Are we reflashing?
  1102. *
  1103. * If we reflash, we could come back as something entirely
  1104. * different, including a non-CAPI card. As such, by default
  1105. * we don't participate in the process. We'll be unbound and
  1106. * the slot re-probed. (TODO: check EEH doesn't blindly rebind
  1107. * us!)
  1108. *
  1109. * However, this isn't the entire story: for reliablity
  1110. * reasons, we usually want to reflash the FPGA on PERST in
  1111. * order to get back to a more reliable known-good state.
  1112. *
  1113. * This causes us a bit of a problem: if we reflash we can't
  1114. * trust that we'll come back the same - we could have a new
  1115. * image and been PERSTed in order to load that
  1116. * image. However, most of the time we actually *will* come
  1117. * back the same - for example a regular EEH event.
  1118. *
  1119. * Therefore, we allow the user to assert that the image is
  1120. * indeed the same and that we should continue on into EEH
  1121. * anyway.
  1122. */
  1123. if (adapter->perst_loads_image && !adapter->perst_same_image) {
  1124. /* TODO take the PHB out of CXL mode */
  1125. dev_info(&pdev->dev, "reflashing, so opting out of EEH!\n");
  1126. return PCI_ERS_RESULT_NONE;
  1127. }
  1128. /*
  1129. * At this point, we want to try to recover. We'll always
  1130. * need a complete slot reset: we don't trust any other reset.
  1131. *
  1132. * Now, we go through each AFU:
  1133. * - We send the driver, if bound, an error_detected callback.
  1134. * We expect it to clean up, but it can also tell us to give
  1135. * up and permanently detach the card. To simplify things, if
  1136. * any bound AFU driver doesn't support EEH, we give up on EEH.
  1137. *
  1138. * - We detach all contexts associated with the AFU. This
  1139. * does not free them, but puts them into a CLOSED state
  1140. * which causes any the associated files to return useful
  1141. * errors to userland. It also unmaps, but does not free,
  1142. * any IRQs.
  1143. *
  1144. * - We clean up our side: releasing and unmapping resources we hold
  1145. * so we can wire them up again when the hardware comes back up.
  1146. *
  1147. * Driver authors should note:
  1148. *
  1149. * - Any contexts you create in your kernel driver (except
  1150. * those associated with anonymous file descriptors) are
  1151. * your responsibility to free and recreate. Likewise with
  1152. * any attached resources.
  1153. *
  1154. * - We will take responsibility for re-initialising the
  1155. * device context (the one set up for you in
  1156. * cxl_pci_enable_device_hook and accessed through
  1157. * cxl_get_context). If you've attached IRQs or other
  1158. * resources to it, they remains yours to free.
  1159. *
  1160. * You can call the same functions to release resources as you
  1161. * normally would: we make sure that these functions continue
  1162. * to work when the hardware is down.
  1163. *
  1164. * Two examples:
  1165. *
  1166. * 1) If you normally free all your resources at the end of
  1167. * each request, or if you use anonymous FDs, your
  1168. * error_detected callback can simply set a flag to tell
  1169. * your driver not to start any new calls. You can then
  1170. * clear the flag in the resume callback.
  1171. *
  1172. * 2) If you normally allocate your resources on startup:
  1173. * * Set a flag in error_detected as above.
  1174. * * Let CXL detach your contexts.
  1175. * * In slot_reset, free the old resources and allocate new ones.
  1176. * * In resume, clear the flag to allow things to start.
  1177. */
  1178. for (i = 0; i < adapter->slices; i++) {
  1179. afu = adapter->afu[i];
  1180. result = cxl_vphb_error_detected(afu, state);
  1181. /* Only continue if everyone agrees on NEED_RESET */
  1182. if (result != PCI_ERS_RESULT_NEED_RESET)
  1183. return result;
  1184. cxl_context_detach_all(afu);
  1185. cxl_ops->afu_deactivate_mode(afu, afu->current_mode);
  1186. pci_deconfigure_afu(afu);
  1187. }
  1188. cxl_deconfigure_adapter(adapter);
  1189. return result;
  1190. }
  1191. static pci_ers_result_t cxl_pci_slot_reset(struct pci_dev *pdev)
  1192. {
  1193. struct cxl *adapter = pci_get_drvdata(pdev);
  1194. struct cxl_afu *afu;
  1195. struct cxl_context *ctx;
  1196. struct pci_dev *afu_dev;
  1197. pci_ers_result_t afu_result = PCI_ERS_RESULT_RECOVERED;
  1198. pci_ers_result_t result = PCI_ERS_RESULT_RECOVERED;
  1199. int i;
  1200. if (cxl_configure_adapter(adapter, pdev))
  1201. goto err;
  1202. for (i = 0; i < adapter->slices; i++) {
  1203. afu = adapter->afu[i];
  1204. if (pci_configure_afu(afu, adapter, pdev))
  1205. goto err;
  1206. if (cxl_afu_select_best_mode(afu))
  1207. goto err;
  1208. list_for_each_entry(afu_dev, &afu->phb->bus->devices, bus_list) {
  1209. /* Reset the device context.
  1210. * TODO: make this less disruptive
  1211. */
  1212. ctx = cxl_get_context(afu_dev);
  1213. if (ctx && cxl_release_context(ctx))
  1214. goto err;
  1215. ctx = cxl_dev_context_init(afu_dev);
  1216. if (!ctx)
  1217. goto err;
  1218. afu_dev->dev.archdata.cxl_ctx = ctx;
  1219. if (cxl_ops->afu_check_and_enable(afu))
  1220. goto err;
  1221. afu_dev->error_state = pci_channel_io_normal;
  1222. /* If there's a driver attached, allow it to
  1223. * chime in on recovery. Drivers should check
  1224. * if everything has come back OK, but
  1225. * shouldn't start new work until we call
  1226. * their resume function.
  1227. */
  1228. if (!afu_dev->driver)
  1229. continue;
  1230. if (afu_dev->driver->err_handler &&
  1231. afu_dev->driver->err_handler->slot_reset)
  1232. afu_result = afu_dev->driver->err_handler->slot_reset(afu_dev);
  1233. if (afu_result == PCI_ERS_RESULT_DISCONNECT)
  1234. result = PCI_ERS_RESULT_DISCONNECT;
  1235. }
  1236. }
  1237. return result;
  1238. err:
  1239. /* All the bits that happen in both error_detected and cxl_remove
  1240. * should be idempotent, so we don't need to worry about leaving a mix
  1241. * of unconfigured and reconfigured resources.
  1242. */
  1243. dev_err(&pdev->dev, "EEH recovery failed. Asking to be disconnected.\n");
  1244. return PCI_ERS_RESULT_DISCONNECT;
  1245. }
  1246. static void cxl_pci_resume(struct pci_dev *pdev)
  1247. {
  1248. struct cxl *adapter = pci_get_drvdata(pdev);
  1249. struct cxl_afu *afu;
  1250. struct pci_dev *afu_dev;
  1251. int i;
  1252. /* Everything is back now. Drivers should restart work now.
  1253. * This is not the place to be checking if everything came back up
  1254. * properly, because there's no return value: do that in slot_reset.
  1255. */
  1256. for (i = 0; i < adapter->slices; i++) {
  1257. afu = adapter->afu[i];
  1258. list_for_each_entry(afu_dev, &afu->phb->bus->devices, bus_list) {
  1259. if (afu_dev->driver && afu_dev->driver->err_handler &&
  1260. afu_dev->driver->err_handler->resume)
  1261. afu_dev->driver->err_handler->resume(afu_dev);
  1262. }
  1263. }
  1264. }
  1265. static const struct pci_error_handlers cxl_err_handler = {
  1266. .error_detected = cxl_pci_error_detected,
  1267. .slot_reset = cxl_pci_slot_reset,
  1268. .resume = cxl_pci_resume,
  1269. };
  1270. struct pci_driver cxl_pci_driver = {
  1271. .name = "cxl-pci",
  1272. .id_table = cxl_pci_tbl,
  1273. .probe = cxl_probe,
  1274. .remove = cxl_remove,
  1275. .shutdown = cxl_remove,
  1276. .err_handler = &cxl_err_handler,
  1277. };