pci.c 41 KB

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