pci.c 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520
  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->afu_desc_mmio + off)
  82. #define AFUD_READ_LE(afu, off) in_le64(afu->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->ps_off + (afu->slice * adapter->ps_size));
  464. afu_desc = p2_base(dev) + adapter->afu_desc_off + (afu->slice * adapter->afu_desc_size);
  465. if (!(afu->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->afu_desc_mmio = ioremap(afu_desc, adapter->afu_desc_size)))
  471. goto err2;
  472. }
  473. return 0;
  474. err2:
  475. iounmap(afu->p2n_mmio);
  476. err1:
  477. iounmap(afu->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->p1n_mmio) {
  489. iounmap(afu->p1n_mmio);
  490. afu->p1n_mmio = NULL;
  491. }
  492. if (afu->afu_desc_mmio) {
  493. iounmap(afu->afu_desc_mmio);
  494. afu->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);
  504. }
  505. /* Expects AFU struct to have recently been zeroed out */
  506. static int cxl_read_afu_descriptor(struct cxl_afu *afu)
  507. {
  508. u64 val;
  509. val = AFUD_READ_INFO(afu);
  510. afu->pp_irqs = AFUD_NUM_INTS_PER_PROC(val);
  511. afu->max_procs_virtualised = AFUD_NUM_PROCS(val);
  512. afu->crs_num = AFUD_NUM_CRS(val);
  513. if (AFUD_AFU_DIRECTED(val))
  514. afu->modes_supported |= CXL_MODE_DIRECTED;
  515. if (AFUD_DEDICATED_PROCESS(val))
  516. afu->modes_supported |= CXL_MODE_DEDICATED;
  517. if (AFUD_TIME_SLICED(val))
  518. afu->modes_supported |= CXL_MODE_TIME_SLICED;
  519. val = AFUD_READ_PPPSA(afu);
  520. afu->pp_size = AFUD_PPPSA_LEN(val) * 4096;
  521. afu->psa = AFUD_PPPSA_PSA(val);
  522. if ((afu->pp_psa = AFUD_PPPSA_PP(val)))
  523. afu->pp_offset = AFUD_READ_PPPSA_OFF(afu);
  524. val = AFUD_READ_CR(afu);
  525. afu->crs_len = AFUD_CR_LEN(val) * 256;
  526. afu->crs_offset = AFUD_READ_CR_OFF(afu);
  527. /* eb_len is in multiple of 4K */
  528. afu->eb_len = AFUD_EB_LEN(AFUD_READ_EB(afu)) * 4096;
  529. afu->eb_offset = AFUD_READ_EB_OFF(afu);
  530. /* eb_off is 4K aligned so lower 12 bits are always zero */
  531. if (EXTRACT_PPC_BITS(afu->eb_offset, 0, 11) != 0) {
  532. dev_warn(&afu->dev,
  533. "Invalid AFU error buffer offset %Lx\n",
  534. afu->eb_offset);
  535. dev_info(&afu->dev,
  536. "Ignoring AFU error buffer in the descriptor\n");
  537. /* indicate that no afu buffer exists */
  538. afu->eb_len = 0;
  539. }
  540. return 0;
  541. }
  542. static int cxl_afu_descriptor_looks_ok(struct cxl_afu *afu)
  543. {
  544. int i, rc;
  545. u32 val;
  546. if (afu->psa && afu->adapter->ps_size <
  547. (afu->pp_offset + afu->pp_size*afu->max_procs_virtualised)) {
  548. dev_err(&afu->dev, "per-process PSA can't fit inside the PSA!\n");
  549. return -ENODEV;
  550. }
  551. if (afu->pp_psa && (afu->pp_size < PAGE_SIZE))
  552. dev_warn(&afu->dev, "AFU uses < PAGE_SIZE per-process PSA!");
  553. for (i = 0; i < afu->crs_num; i++) {
  554. rc = cxl_ops->afu_cr_read32(afu, i, 0, &val);
  555. if (rc || val == 0) {
  556. dev_err(&afu->dev, "ABORTING: AFU configuration record %i is invalid\n", i);
  557. return -EINVAL;
  558. }
  559. }
  560. return 0;
  561. }
  562. static int sanitise_afu_regs(struct cxl_afu *afu)
  563. {
  564. u64 reg;
  565. /*
  566. * Clear out any regs that contain either an IVTE or address or may be
  567. * waiting on an acknowledgement to try to be a bit safer as we bring
  568. * it online
  569. */
  570. reg = cxl_p2n_read(afu, CXL_AFU_Cntl_An);
  571. if ((reg & CXL_AFU_Cntl_An_ES_MASK) != CXL_AFU_Cntl_An_ES_Disabled) {
  572. dev_warn(&afu->dev, "WARNING: AFU was not disabled: %#016llx\n", reg);
  573. if (cxl_ops->afu_reset(afu))
  574. return -EIO;
  575. if (cxl_afu_disable(afu))
  576. return -EIO;
  577. if (cxl_psl_purge(afu))
  578. return -EIO;
  579. }
  580. cxl_p1n_write(afu, CXL_PSL_SPAP_An, 0x0000000000000000);
  581. cxl_p1n_write(afu, CXL_PSL_IVTE_Limit_An, 0x0000000000000000);
  582. cxl_p1n_write(afu, CXL_PSL_IVTE_Offset_An, 0x0000000000000000);
  583. cxl_p1n_write(afu, CXL_PSL_AMBAR_An, 0x0000000000000000);
  584. cxl_p1n_write(afu, CXL_PSL_SPOffset_An, 0x0000000000000000);
  585. cxl_p1n_write(afu, CXL_HAURP_An, 0x0000000000000000);
  586. cxl_p2n_write(afu, CXL_CSRP_An, 0x0000000000000000);
  587. cxl_p2n_write(afu, CXL_AURP1_An, 0x0000000000000000);
  588. cxl_p2n_write(afu, CXL_AURP0_An, 0x0000000000000000);
  589. cxl_p2n_write(afu, CXL_SSTP1_An, 0x0000000000000000);
  590. cxl_p2n_write(afu, CXL_SSTP0_An, 0x0000000000000000);
  591. reg = cxl_p2n_read(afu, CXL_PSL_DSISR_An);
  592. if (reg) {
  593. dev_warn(&afu->dev, "AFU had pending DSISR: %#016llx\n", reg);
  594. if (reg & CXL_PSL_DSISR_TRANS)
  595. cxl_p2n_write(afu, CXL_PSL_TFC_An, CXL_PSL_TFC_An_AE);
  596. else
  597. cxl_p2n_write(afu, CXL_PSL_TFC_An, CXL_PSL_TFC_An_A);
  598. }
  599. reg = cxl_p1n_read(afu, CXL_PSL_SERR_An);
  600. if (reg) {
  601. if (reg & ~0xffff)
  602. dev_warn(&afu->dev, "AFU had pending SERR: %#016llx\n", reg);
  603. cxl_p1n_write(afu, CXL_PSL_SERR_An, reg & ~0xffff);
  604. }
  605. reg = cxl_p2n_read(afu, CXL_PSL_ErrStat_An);
  606. if (reg) {
  607. dev_warn(&afu->dev, "AFU had pending error status: %#016llx\n", reg);
  608. cxl_p2n_write(afu, CXL_PSL_ErrStat_An, reg);
  609. }
  610. return 0;
  611. }
  612. #define ERR_BUFF_MAX_COPY_SIZE PAGE_SIZE
  613. /*
  614. * afu_eb_read:
  615. * Called from sysfs and reads the afu error info buffer. The h/w only supports
  616. * 4/8 bytes aligned access. So in case the requested offset/count arent 8 byte
  617. * aligned the function uses a bounce buffer which can be max PAGE_SIZE.
  618. */
  619. ssize_t cxl_pci_afu_read_err_buffer(struct cxl_afu *afu, char *buf,
  620. loff_t off, size_t count)
  621. {
  622. loff_t aligned_start, aligned_end;
  623. size_t aligned_length;
  624. void *tbuf;
  625. const void __iomem *ebuf = afu->afu_desc_mmio + afu->eb_offset;
  626. if (count == 0 || off < 0 || (size_t)off >= afu->eb_len)
  627. return 0;
  628. /* calculate aligned read window */
  629. count = min((size_t)(afu->eb_len - off), count);
  630. aligned_start = round_down(off, 8);
  631. aligned_end = round_up(off + count, 8);
  632. aligned_length = aligned_end - aligned_start;
  633. /* max we can copy in one read is PAGE_SIZE */
  634. if (aligned_length > ERR_BUFF_MAX_COPY_SIZE) {
  635. aligned_length = ERR_BUFF_MAX_COPY_SIZE;
  636. count = ERR_BUFF_MAX_COPY_SIZE - (off & 0x7);
  637. }
  638. /* use bounce buffer for copy */
  639. tbuf = (void *)__get_free_page(GFP_TEMPORARY);
  640. if (!tbuf)
  641. return -ENOMEM;
  642. /* perform aligned read from the mmio region */
  643. memcpy_fromio(tbuf, ebuf + aligned_start, aligned_length);
  644. memcpy(buf, tbuf + (off & 0x7), count);
  645. free_page((unsigned long)tbuf);
  646. return count;
  647. }
  648. static int pci_configure_afu(struct cxl_afu *afu, struct cxl *adapter, struct pci_dev *dev)
  649. {
  650. int rc;
  651. if ((rc = pci_map_slice_regs(afu, adapter, dev)))
  652. return rc;
  653. if ((rc = sanitise_afu_regs(afu)))
  654. goto err1;
  655. /* We need to reset the AFU before we can read the AFU descriptor */
  656. if ((rc = cxl_ops->afu_reset(afu)))
  657. goto err1;
  658. if (cxl_verbose)
  659. dump_afu_descriptor(afu);
  660. if ((rc = cxl_read_afu_descriptor(afu)))
  661. goto err1;
  662. if ((rc = cxl_afu_descriptor_looks_ok(afu)))
  663. goto err1;
  664. if ((rc = init_implementation_afu_regs(afu)))
  665. goto err1;
  666. if ((rc = cxl_native_register_serr_irq(afu)))
  667. goto err1;
  668. if ((rc = cxl_native_register_psl_irq(afu)))
  669. goto err2;
  670. return 0;
  671. err2:
  672. cxl_native_release_serr_irq(afu);
  673. err1:
  674. pci_unmap_slice_regs(afu);
  675. return rc;
  676. }
  677. static void pci_deconfigure_afu(struct cxl_afu *afu)
  678. {
  679. cxl_native_release_psl_irq(afu);
  680. cxl_native_release_serr_irq(afu);
  681. pci_unmap_slice_regs(afu);
  682. }
  683. static int pci_init_afu(struct cxl *adapter, int slice, struct pci_dev *dev)
  684. {
  685. struct cxl_afu *afu;
  686. int rc;
  687. afu = cxl_alloc_afu(adapter, slice);
  688. if (!afu)
  689. return -ENOMEM;
  690. rc = dev_set_name(&afu->dev, "afu%i.%i", adapter->adapter_num, slice);
  691. if (rc)
  692. goto err_free;
  693. rc = pci_configure_afu(afu, adapter, dev);
  694. if (rc)
  695. goto err_free;
  696. /* Don't care if this fails */
  697. cxl_debugfs_afu_add(afu);
  698. /*
  699. * After we call this function we must not free the afu directly, even
  700. * if it returns an error!
  701. */
  702. if ((rc = cxl_register_afu(afu)))
  703. goto err_put1;
  704. if ((rc = cxl_sysfs_afu_add(afu)))
  705. goto err_put1;
  706. adapter->afu[afu->slice] = afu;
  707. if ((rc = cxl_pci_vphb_add(afu)))
  708. dev_info(&afu->dev, "Can't register vPHB\n");
  709. return 0;
  710. err_put1:
  711. pci_deconfigure_afu(afu);
  712. cxl_debugfs_afu_remove(afu);
  713. device_unregister(&afu->dev);
  714. return rc;
  715. err_free:
  716. kfree(afu);
  717. return rc;
  718. }
  719. static void cxl_pci_remove_afu(struct cxl_afu *afu)
  720. {
  721. pr_devel("%s\n", __func__);
  722. if (!afu)
  723. return;
  724. cxl_sysfs_afu_remove(afu);
  725. cxl_debugfs_afu_remove(afu);
  726. spin_lock(&afu->adapter->afu_list_lock);
  727. afu->adapter->afu[afu->slice] = NULL;
  728. spin_unlock(&afu->adapter->afu_list_lock);
  729. cxl_context_detach_all(afu);
  730. cxl_ops->afu_deactivate_mode(afu, afu->current_mode);
  731. pci_deconfigure_afu(afu);
  732. device_unregister(&afu->dev);
  733. }
  734. int cxl_pci_reset(struct cxl *adapter)
  735. {
  736. struct pci_dev *dev = to_pci_dev(adapter->dev.parent);
  737. int rc;
  738. if (adapter->perst_same_image) {
  739. dev_warn(&dev->dev,
  740. "cxl: refusing to reset/reflash when perst_reloads_same_image is set.\n");
  741. return -EINVAL;
  742. }
  743. dev_info(&dev->dev, "CXL reset\n");
  744. /* pcie_warm_reset requests a fundamental pci reset which includes a
  745. * PERST assert/deassert. PERST triggers a loading of the image
  746. * if "user" or "factory" is selected in sysfs */
  747. if ((rc = pci_set_pcie_reset_state(dev, pcie_warm_reset))) {
  748. dev_err(&dev->dev, "cxl: pcie_warm_reset failed\n");
  749. return rc;
  750. }
  751. return rc;
  752. }
  753. static int cxl_map_adapter_regs(struct cxl *adapter, struct pci_dev *dev)
  754. {
  755. if (pci_request_region(dev, 2, "priv 2 regs"))
  756. goto err1;
  757. if (pci_request_region(dev, 0, "priv 1 regs"))
  758. goto err2;
  759. pr_devel("cxl_map_adapter_regs: p1: %#016llx %#llx, p2: %#016llx %#llx",
  760. p1_base(dev), p1_size(dev), p2_base(dev), p2_size(dev));
  761. if (!(adapter->p1_mmio = ioremap(p1_base(dev), p1_size(dev))))
  762. goto err3;
  763. if (!(adapter->p2_mmio = ioremap(p2_base(dev), p2_size(dev))))
  764. goto err4;
  765. return 0;
  766. err4:
  767. iounmap(adapter->p1_mmio);
  768. adapter->p1_mmio = NULL;
  769. err3:
  770. pci_release_region(dev, 0);
  771. err2:
  772. pci_release_region(dev, 2);
  773. err1:
  774. return -ENOMEM;
  775. }
  776. static void cxl_unmap_adapter_regs(struct cxl *adapter)
  777. {
  778. if (adapter->p1_mmio) {
  779. iounmap(adapter->p1_mmio);
  780. adapter->p1_mmio = NULL;
  781. pci_release_region(to_pci_dev(adapter->dev.parent), 2);
  782. }
  783. if (adapter->p2_mmio) {
  784. iounmap(adapter->p2_mmio);
  785. adapter->p2_mmio = NULL;
  786. pci_release_region(to_pci_dev(adapter->dev.parent), 0);
  787. }
  788. }
  789. static int cxl_read_vsec(struct cxl *adapter, struct pci_dev *dev)
  790. {
  791. int vsec;
  792. u32 afu_desc_off, afu_desc_size;
  793. u32 ps_off, ps_size;
  794. u16 vseclen;
  795. u8 image_state;
  796. if (!(vsec = find_cxl_vsec(dev))) {
  797. dev_err(&dev->dev, "ABORTING: CXL VSEC not found!\n");
  798. return -ENODEV;
  799. }
  800. CXL_READ_VSEC_LENGTH(dev, vsec, &vseclen);
  801. if (vseclen < CXL_VSEC_MIN_SIZE) {
  802. dev_err(&dev->dev, "ABORTING: CXL VSEC too short\n");
  803. return -EINVAL;
  804. }
  805. CXL_READ_VSEC_STATUS(dev, vsec, &adapter->vsec_status);
  806. CXL_READ_VSEC_PSL_REVISION(dev, vsec, &adapter->psl_rev);
  807. CXL_READ_VSEC_CAIA_MAJOR(dev, vsec, &adapter->caia_major);
  808. CXL_READ_VSEC_CAIA_MINOR(dev, vsec, &adapter->caia_minor);
  809. CXL_READ_VSEC_BASE_IMAGE(dev, vsec, &adapter->base_image);
  810. CXL_READ_VSEC_IMAGE_STATE(dev, vsec, &image_state);
  811. adapter->user_image_loaded = !!(image_state & CXL_VSEC_USER_IMAGE_LOADED);
  812. adapter->perst_select_user = !!(image_state & CXL_VSEC_USER_IMAGE_LOADED);
  813. CXL_READ_VSEC_NAFUS(dev, vsec, &adapter->slices);
  814. CXL_READ_VSEC_AFU_DESC_OFF(dev, vsec, &afu_desc_off);
  815. CXL_READ_VSEC_AFU_DESC_SIZE(dev, vsec, &afu_desc_size);
  816. CXL_READ_VSEC_PS_OFF(dev, vsec, &ps_off);
  817. CXL_READ_VSEC_PS_SIZE(dev, vsec, &ps_size);
  818. /* Convert everything to bytes, because there is NO WAY I'd look at the
  819. * code a month later and forget what units these are in ;-) */
  820. adapter->ps_off = ps_off * 64 * 1024;
  821. adapter->ps_size = ps_size * 64 * 1024;
  822. adapter->afu_desc_off = afu_desc_off * 64 * 1024;
  823. adapter->afu_desc_size = afu_desc_size *64 * 1024;
  824. /* Total IRQs - 1 PSL ERROR - #AFU*(1 slice error + 1 DSI) */
  825. adapter->user_irqs = pnv_cxl_get_irq_count(dev) - 1 - 2*adapter->slices;
  826. return 0;
  827. }
  828. /*
  829. * Workaround a PCIe Host Bridge defect on some cards, that can cause
  830. * malformed Transaction Layer Packet (TLP) errors to be erroneously
  831. * reported. Mask this error in the Uncorrectable Error Mask Register.
  832. *
  833. * The upper nibble of the PSL revision is used to distinguish between
  834. * different cards. The affected ones have it set to 0.
  835. */
  836. static void cxl_fixup_malformed_tlp(struct cxl *adapter, struct pci_dev *dev)
  837. {
  838. int aer;
  839. u32 data;
  840. if (adapter->psl_rev & 0xf000)
  841. return;
  842. if (!(aer = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ERR)))
  843. return;
  844. pci_read_config_dword(dev, aer + PCI_ERR_UNCOR_MASK, &data);
  845. if (data & PCI_ERR_UNC_MALF_TLP)
  846. if (data & PCI_ERR_UNC_INTN)
  847. return;
  848. data |= PCI_ERR_UNC_MALF_TLP;
  849. data |= PCI_ERR_UNC_INTN;
  850. pci_write_config_dword(dev, aer + PCI_ERR_UNCOR_MASK, data);
  851. }
  852. static int cxl_vsec_looks_ok(struct cxl *adapter, struct pci_dev *dev)
  853. {
  854. if (adapter->vsec_status & CXL_STATUS_SECOND_PORT)
  855. return -EBUSY;
  856. if (adapter->vsec_status & CXL_UNSUPPORTED_FEATURES) {
  857. dev_err(&dev->dev, "ABORTING: CXL requires unsupported features\n");
  858. return -EINVAL;
  859. }
  860. if (!adapter->slices) {
  861. /* Once we support dynamic reprogramming we can use the card if
  862. * it supports loadable AFUs */
  863. dev_err(&dev->dev, "ABORTING: Device has no AFUs\n");
  864. return -EINVAL;
  865. }
  866. if (!adapter->afu_desc_off || !adapter->afu_desc_size) {
  867. dev_err(&dev->dev, "ABORTING: VSEC shows no AFU descriptors\n");
  868. return -EINVAL;
  869. }
  870. if (adapter->ps_size > p2_size(dev) - adapter->ps_off) {
  871. dev_err(&dev->dev, "ABORTING: Problem state size larger than "
  872. "available in BAR2: 0x%llx > 0x%llx\n",
  873. adapter->ps_size, p2_size(dev) - adapter->ps_off);
  874. return -EINVAL;
  875. }
  876. return 0;
  877. }
  878. static void cxl_release_adapter(struct device *dev)
  879. {
  880. struct cxl *adapter = to_cxl_adapter(dev);
  881. pr_devel("cxl_release_adapter\n");
  882. cxl_remove_adapter_nr(adapter);
  883. kfree(adapter);
  884. }
  885. #define CXL_PSL_ErrIVTE_tberror (0x1ull << (63-31))
  886. static int sanitise_adapter_regs(struct cxl *adapter)
  887. {
  888. /* Clear PSL tberror bit by writing 1 to it */
  889. cxl_p1_write(adapter, CXL_PSL_ErrIVTE, CXL_PSL_ErrIVTE_tberror);
  890. return cxl_tlb_slb_invalidate(adapter);
  891. }
  892. /* This should contain *only* operations that can safely be done in
  893. * both creation and recovery.
  894. */
  895. static int cxl_configure_adapter(struct cxl *adapter, struct pci_dev *dev)
  896. {
  897. int rc;
  898. adapter->dev.parent = &dev->dev;
  899. adapter->dev.release = cxl_release_adapter;
  900. pci_set_drvdata(dev, adapter);
  901. rc = pci_enable_device(dev);
  902. if (rc) {
  903. dev_err(&dev->dev, "pci_enable_device failed: %i\n", rc);
  904. return rc;
  905. }
  906. if ((rc = cxl_read_vsec(adapter, dev)))
  907. return rc;
  908. if ((rc = cxl_vsec_looks_ok(adapter, dev)))
  909. return rc;
  910. cxl_fixup_malformed_tlp(adapter, dev);
  911. if ((rc = setup_cxl_bars(dev)))
  912. return rc;
  913. if ((rc = switch_card_to_cxl(dev)))
  914. return rc;
  915. if ((rc = cxl_update_image_control(adapter)))
  916. return rc;
  917. if ((rc = cxl_map_adapter_regs(adapter, dev)))
  918. return rc;
  919. if ((rc = sanitise_adapter_regs(adapter)))
  920. goto err;
  921. if ((rc = init_implementation_adapter_regs(adapter, dev)))
  922. goto err;
  923. if ((rc = pnv_phb_to_cxl_mode(dev, OPAL_PHB_CAPI_MODE_CAPI)))
  924. goto err;
  925. /* If recovery happened, the last step is to turn on snooping.
  926. * In the non-recovery case this has no effect */
  927. if ((rc = pnv_phb_to_cxl_mode(dev, OPAL_PHB_CAPI_MODE_SNOOP_ON)))
  928. goto err;
  929. if ((rc = cxl_setup_psl_timebase(adapter, dev)))
  930. goto err;
  931. if ((rc = cxl_native_register_psl_err_irq(adapter)))
  932. goto err;
  933. return 0;
  934. err:
  935. cxl_unmap_adapter_regs(adapter);
  936. return rc;
  937. }
  938. static void cxl_deconfigure_adapter(struct cxl *adapter)
  939. {
  940. struct pci_dev *pdev = to_pci_dev(adapter->dev.parent);
  941. cxl_native_release_psl_err_irq(adapter);
  942. cxl_unmap_adapter_regs(adapter);
  943. pci_disable_device(pdev);
  944. }
  945. static struct cxl *cxl_pci_init_adapter(struct pci_dev *dev)
  946. {
  947. struct cxl *adapter;
  948. int rc;
  949. adapter = cxl_alloc_adapter();
  950. if (!adapter)
  951. return ERR_PTR(-ENOMEM);
  952. /* Set defaults for parameters which need to persist over
  953. * configure/reconfigure
  954. */
  955. adapter->perst_loads_image = true;
  956. adapter->perst_same_image = false;
  957. rc = cxl_configure_adapter(adapter, dev);
  958. if (rc) {
  959. pci_disable_device(dev);
  960. cxl_release_adapter(&adapter->dev);
  961. return ERR_PTR(rc);
  962. }
  963. /* Don't care if this one fails: */
  964. cxl_debugfs_adapter_add(adapter);
  965. /*
  966. * After we call this function we must not free the adapter directly,
  967. * even if it returns an error!
  968. */
  969. if ((rc = cxl_register_adapter(adapter)))
  970. goto err_put1;
  971. if ((rc = cxl_sysfs_adapter_add(adapter)))
  972. goto err_put1;
  973. return adapter;
  974. err_put1:
  975. /* This should mirror cxl_remove_adapter, except without the
  976. * sysfs parts
  977. */
  978. cxl_debugfs_adapter_remove(adapter);
  979. cxl_deconfigure_adapter(adapter);
  980. device_unregister(&adapter->dev);
  981. return ERR_PTR(rc);
  982. }
  983. static void cxl_pci_remove_adapter(struct cxl *adapter)
  984. {
  985. pr_devel("cxl_remove_adapter\n");
  986. cxl_sysfs_adapter_remove(adapter);
  987. cxl_debugfs_adapter_remove(adapter);
  988. cxl_deconfigure_adapter(adapter);
  989. device_unregister(&adapter->dev);
  990. }
  991. static int cxl_probe(struct pci_dev *dev, const struct pci_device_id *id)
  992. {
  993. struct cxl *adapter;
  994. int slice;
  995. int rc;
  996. if (cxl_verbose)
  997. dump_cxl_config_space(dev);
  998. adapter = cxl_pci_init_adapter(dev);
  999. if (IS_ERR(adapter)) {
  1000. dev_err(&dev->dev, "cxl_init_adapter failed: %li\n", PTR_ERR(adapter));
  1001. return PTR_ERR(adapter);
  1002. }
  1003. for (slice = 0; slice < adapter->slices; slice++) {
  1004. if ((rc = pci_init_afu(adapter, slice, dev))) {
  1005. dev_err(&dev->dev, "AFU %i failed to initialise: %i\n", slice, rc);
  1006. continue;
  1007. }
  1008. rc = cxl_afu_select_best_mode(adapter->afu[slice]);
  1009. if (rc)
  1010. dev_err(&dev->dev, "AFU %i failed to start: %i\n", slice, rc);
  1011. }
  1012. return 0;
  1013. }
  1014. static void cxl_remove(struct pci_dev *dev)
  1015. {
  1016. struct cxl *adapter = pci_get_drvdata(dev);
  1017. struct cxl_afu *afu;
  1018. int i;
  1019. /*
  1020. * Lock to prevent someone grabbing a ref through the adapter list as
  1021. * we are removing it
  1022. */
  1023. for (i = 0; i < adapter->slices; i++) {
  1024. afu = adapter->afu[i];
  1025. cxl_pci_vphb_remove(afu);
  1026. cxl_pci_remove_afu(afu);
  1027. }
  1028. cxl_pci_remove_adapter(adapter);
  1029. }
  1030. static pci_ers_result_t cxl_vphb_error_detected(struct cxl_afu *afu,
  1031. pci_channel_state_t state)
  1032. {
  1033. struct pci_dev *afu_dev;
  1034. pci_ers_result_t result = PCI_ERS_RESULT_NEED_RESET;
  1035. pci_ers_result_t afu_result = PCI_ERS_RESULT_NEED_RESET;
  1036. /* There should only be one entry, but go through the list
  1037. * anyway
  1038. */
  1039. list_for_each_entry(afu_dev, &afu->phb->bus->devices, bus_list) {
  1040. if (!afu_dev->driver)
  1041. continue;
  1042. afu_dev->error_state = state;
  1043. if (afu_dev->driver->err_handler)
  1044. afu_result = afu_dev->driver->err_handler->error_detected(afu_dev,
  1045. state);
  1046. /* Disconnect trumps all, NONE trumps NEED_RESET */
  1047. if (afu_result == PCI_ERS_RESULT_DISCONNECT)
  1048. result = PCI_ERS_RESULT_DISCONNECT;
  1049. else if ((afu_result == PCI_ERS_RESULT_NONE) &&
  1050. (result == PCI_ERS_RESULT_NEED_RESET))
  1051. result = PCI_ERS_RESULT_NONE;
  1052. }
  1053. return result;
  1054. }
  1055. static pci_ers_result_t cxl_pci_error_detected(struct pci_dev *pdev,
  1056. pci_channel_state_t state)
  1057. {
  1058. struct cxl *adapter = pci_get_drvdata(pdev);
  1059. struct cxl_afu *afu;
  1060. pci_ers_result_t result = PCI_ERS_RESULT_NEED_RESET;
  1061. int i;
  1062. /* At this point, we could still have an interrupt pending.
  1063. * Let's try to get them out of the way before they do
  1064. * anything we don't like.
  1065. */
  1066. schedule();
  1067. /* If we're permanently dead, give up. */
  1068. if (state == pci_channel_io_perm_failure) {
  1069. /* Tell the AFU drivers; but we don't care what they
  1070. * say, we're going away.
  1071. */
  1072. for (i = 0; i < adapter->slices; i++) {
  1073. afu = adapter->afu[i];
  1074. cxl_vphb_error_detected(afu, state);
  1075. }
  1076. return PCI_ERS_RESULT_DISCONNECT;
  1077. }
  1078. /* Are we reflashing?
  1079. *
  1080. * If we reflash, we could come back as something entirely
  1081. * different, including a non-CAPI card. As such, by default
  1082. * we don't participate in the process. We'll be unbound and
  1083. * the slot re-probed. (TODO: check EEH doesn't blindly rebind
  1084. * us!)
  1085. *
  1086. * However, this isn't the entire story: for reliablity
  1087. * reasons, we usually want to reflash the FPGA on PERST in
  1088. * order to get back to a more reliable known-good state.
  1089. *
  1090. * This causes us a bit of a problem: if we reflash we can't
  1091. * trust that we'll come back the same - we could have a new
  1092. * image and been PERSTed in order to load that
  1093. * image. However, most of the time we actually *will* come
  1094. * back the same - for example a regular EEH event.
  1095. *
  1096. * Therefore, we allow the user to assert that the image is
  1097. * indeed the same and that we should continue on into EEH
  1098. * anyway.
  1099. */
  1100. if (adapter->perst_loads_image && !adapter->perst_same_image) {
  1101. /* TODO take the PHB out of CXL mode */
  1102. dev_info(&pdev->dev, "reflashing, so opting out of EEH!\n");
  1103. return PCI_ERS_RESULT_NONE;
  1104. }
  1105. /*
  1106. * At this point, we want to try to recover. We'll always
  1107. * need a complete slot reset: we don't trust any other reset.
  1108. *
  1109. * Now, we go through each AFU:
  1110. * - We send the driver, if bound, an error_detected callback.
  1111. * We expect it to clean up, but it can also tell us to give
  1112. * up and permanently detach the card. To simplify things, if
  1113. * any bound AFU driver doesn't support EEH, we give up on EEH.
  1114. *
  1115. * - We detach all contexts associated with the AFU. This
  1116. * does not free them, but puts them into a CLOSED state
  1117. * which causes any the associated files to return useful
  1118. * errors to userland. It also unmaps, but does not free,
  1119. * any IRQs.
  1120. *
  1121. * - We clean up our side: releasing and unmapping resources we hold
  1122. * so we can wire them up again when the hardware comes back up.
  1123. *
  1124. * Driver authors should note:
  1125. *
  1126. * - Any contexts you create in your kernel driver (except
  1127. * those associated with anonymous file descriptors) are
  1128. * your responsibility to free and recreate. Likewise with
  1129. * any attached resources.
  1130. *
  1131. * - We will take responsibility for re-initialising the
  1132. * device context (the one set up for you in
  1133. * cxl_pci_enable_device_hook and accessed through
  1134. * cxl_get_context). If you've attached IRQs or other
  1135. * resources to it, they remains yours to free.
  1136. *
  1137. * You can call the same functions to release resources as you
  1138. * normally would: we make sure that these functions continue
  1139. * to work when the hardware is down.
  1140. *
  1141. * Two examples:
  1142. *
  1143. * 1) If you normally free all your resources at the end of
  1144. * each request, or if you use anonymous FDs, your
  1145. * error_detected callback can simply set a flag to tell
  1146. * your driver not to start any new calls. You can then
  1147. * clear the flag in the resume callback.
  1148. *
  1149. * 2) If you normally allocate your resources on startup:
  1150. * * Set a flag in error_detected as above.
  1151. * * Let CXL detach your contexts.
  1152. * * In slot_reset, free the old resources and allocate new ones.
  1153. * * In resume, clear the flag to allow things to start.
  1154. */
  1155. for (i = 0; i < adapter->slices; i++) {
  1156. afu = adapter->afu[i];
  1157. result = cxl_vphb_error_detected(afu, state);
  1158. /* Only continue if everyone agrees on NEED_RESET */
  1159. if (result != PCI_ERS_RESULT_NEED_RESET)
  1160. return result;
  1161. cxl_context_detach_all(afu);
  1162. cxl_ops->afu_deactivate_mode(afu, afu->current_mode);
  1163. pci_deconfigure_afu(afu);
  1164. }
  1165. cxl_deconfigure_adapter(adapter);
  1166. return result;
  1167. }
  1168. static pci_ers_result_t cxl_pci_slot_reset(struct pci_dev *pdev)
  1169. {
  1170. struct cxl *adapter = pci_get_drvdata(pdev);
  1171. struct cxl_afu *afu;
  1172. struct cxl_context *ctx;
  1173. struct pci_dev *afu_dev;
  1174. pci_ers_result_t afu_result = PCI_ERS_RESULT_RECOVERED;
  1175. pci_ers_result_t result = PCI_ERS_RESULT_RECOVERED;
  1176. int i;
  1177. if (cxl_configure_adapter(adapter, pdev))
  1178. goto err;
  1179. for (i = 0; i < adapter->slices; i++) {
  1180. afu = adapter->afu[i];
  1181. if (pci_configure_afu(afu, adapter, pdev))
  1182. goto err;
  1183. if (cxl_afu_select_best_mode(afu))
  1184. goto err;
  1185. cxl_pci_vphb_reconfigure(afu);
  1186. list_for_each_entry(afu_dev, &afu->phb->bus->devices, bus_list) {
  1187. /* Reset the device context.
  1188. * TODO: make this less disruptive
  1189. */
  1190. ctx = cxl_get_context(afu_dev);
  1191. if (ctx && cxl_release_context(ctx))
  1192. goto err;
  1193. ctx = cxl_dev_context_init(afu_dev);
  1194. if (!ctx)
  1195. goto err;
  1196. afu_dev->dev.archdata.cxl_ctx = ctx;
  1197. if (cxl_ops->afu_check_and_enable(afu))
  1198. goto err;
  1199. afu_dev->error_state = pci_channel_io_normal;
  1200. /* If there's a driver attached, allow it to
  1201. * chime in on recovery. Drivers should check
  1202. * if everything has come back OK, but
  1203. * shouldn't start new work until we call
  1204. * their resume function.
  1205. */
  1206. if (!afu_dev->driver)
  1207. continue;
  1208. if (afu_dev->driver->err_handler &&
  1209. afu_dev->driver->err_handler->slot_reset)
  1210. afu_result = afu_dev->driver->err_handler->slot_reset(afu_dev);
  1211. if (afu_result == PCI_ERS_RESULT_DISCONNECT)
  1212. result = PCI_ERS_RESULT_DISCONNECT;
  1213. }
  1214. }
  1215. return result;
  1216. err:
  1217. /* All the bits that happen in both error_detected and cxl_remove
  1218. * should be idempotent, so we don't need to worry about leaving a mix
  1219. * of unconfigured and reconfigured resources.
  1220. */
  1221. dev_err(&pdev->dev, "EEH recovery failed. Asking to be disconnected.\n");
  1222. return PCI_ERS_RESULT_DISCONNECT;
  1223. }
  1224. static void cxl_pci_resume(struct pci_dev *pdev)
  1225. {
  1226. struct cxl *adapter = pci_get_drvdata(pdev);
  1227. struct cxl_afu *afu;
  1228. struct pci_dev *afu_dev;
  1229. int i;
  1230. /* Everything is back now. Drivers should restart work now.
  1231. * This is not the place to be checking if everything came back up
  1232. * properly, because there's no return value: do that in slot_reset.
  1233. */
  1234. for (i = 0; i < adapter->slices; i++) {
  1235. afu = adapter->afu[i];
  1236. list_for_each_entry(afu_dev, &afu->phb->bus->devices, bus_list) {
  1237. if (afu_dev->driver && afu_dev->driver->err_handler &&
  1238. afu_dev->driver->err_handler->resume)
  1239. afu_dev->driver->err_handler->resume(afu_dev);
  1240. }
  1241. }
  1242. }
  1243. static const struct pci_error_handlers cxl_err_handler = {
  1244. .error_detected = cxl_pci_error_detected,
  1245. .slot_reset = cxl_pci_slot_reset,
  1246. .resume = cxl_pci_resume,
  1247. };
  1248. struct pci_driver cxl_pci_driver = {
  1249. .name = "cxl-pci",
  1250. .id_table = cxl_pci_tbl,
  1251. .probe = cxl_probe,
  1252. .remove = cxl_remove,
  1253. .shutdown = cxl_remove,
  1254. .err_handler = &cxl_err_handler,
  1255. };