guest.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211
  1. /*
  2. * Copyright 2015 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/spinlock.h>
  10. #include <linux/uaccess.h>
  11. #include <linux/delay.h>
  12. #include "cxl.h"
  13. #include "hcalls.h"
  14. #include "trace.h"
  15. #define CXL_ERROR_DETECTED_EVENT 1
  16. #define CXL_SLOT_RESET_EVENT 2
  17. #define CXL_RESUME_EVENT 3
  18. static void pci_error_handlers(struct cxl_afu *afu,
  19. int bus_error_event,
  20. pci_channel_state_t state)
  21. {
  22. struct pci_dev *afu_dev;
  23. if (afu->phb == NULL)
  24. return;
  25. list_for_each_entry(afu_dev, &afu->phb->bus->devices, bus_list) {
  26. if (!afu_dev->driver)
  27. continue;
  28. switch (bus_error_event) {
  29. case CXL_ERROR_DETECTED_EVENT:
  30. afu_dev->error_state = state;
  31. if (afu_dev->driver->err_handler &&
  32. afu_dev->driver->err_handler->error_detected)
  33. afu_dev->driver->err_handler->error_detected(afu_dev, state);
  34. break;
  35. case CXL_SLOT_RESET_EVENT:
  36. afu_dev->error_state = state;
  37. if (afu_dev->driver->err_handler &&
  38. afu_dev->driver->err_handler->slot_reset)
  39. afu_dev->driver->err_handler->slot_reset(afu_dev);
  40. break;
  41. case CXL_RESUME_EVENT:
  42. if (afu_dev->driver->err_handler &&
  43. afu_dev->driver->err_handler->resume)
  44. afu_dev->driver->err_handler->resume(afu_dev);
  45. break;
  46. }
  47. }
  48. }
  49. static irqreturn_t guest_handle_psl_slice_error(struct cxl_context *ctx, u64 dsisr,
  50. u64 errstat)
  51. {
  52. pr_devel("in %s\n", __func__);
  53. dev_crit(&ctx->afu->dev, "PSL ERROR STATUS: 0x%.16llx\n", errstat);
  54. return cxl_ops->ack_irq(ctx, 0, errstat);
  55. }
  56. static ssize_t guest_collect_vpd(struct cxl *adapter, struct cxl_afu *afu,
  57. void *buf, size_t len)
  58. {
  59. unsigned int entries, mod;
  60. unsigned long **vpd_buf = NULL;
  61. struct sg_list *le;
  62. int rc = 0, i, tocopy;
  63. u64 out = 0;
  64. if (buf == NULL)
  65. return -EINVAL;
  66. /* number of entries in the list */
  67. entries = len / SG_BUFFER_SIZE;
  68. mod = len % SG_BUFFER_SIZE;
  69. if (mod)
  70. entries++;
  71. if (entries > SG_MAX_ENTRIES) {
  72. entries = SG_MAX_ENTRIES;
  73. len = SG_MAX_ENTRIES * SG_BUFFER_SIZE;
  74. mod = 0;
  75. }
  76. vpd_buf = kzalloc(entries * sizeof(unsigned long *), GFP_KERNEL);
  77. if (!vpd_buf)
  78. return -ENOMEM;
  79. le = (struct sg_list *)get_zeroed_page(GFP_KERNEL);
  80. if (!le) {
  81. rc = -ENOMEM;
  82. goto err1;
  83. }
  84. for (i = 0; i < entries; i++) {
  85. vpd_buf[i] = (unsigned long *)get_zeroed_page(GFP_KERNEL);
  86. if (!vpd_buf[i]) {
  87. rc = -ENOMEM;
  88. goto err2;
  89. }
  90. le[i].phys_addr = cpu_to_be64(virt_to_phys(vpd_buf[i]));
  91. le[i].len = cpu_to_be64(SG_BUFFER_SIZE);
  92. if ((i == (entries - 1)) && mod)
  93. le[i].len = cpu_to_be64(mod);
  94. }
  95. if (adapter)
  96. rc = cxl_h_collect_vpd_adapter(adapter->guest->handle,
  97. virt_to_phys(le), entries, &out);
  98. else
  99. rc = cxl_h_collect_vpd(afu->guest->handle, 0,
  100. virt_to_phys(le), entries, &out);
  101. pr_devel("length of available (entries: %i), vpd: %#llx\n",
  102. entries, out);
  103. if (!rc) {
  104. /*
  105. * hcall returns in 'out' the size of available VPDs.
  106. * It fills the buffer with as much data as possible.
  107. */
  108. if (out < len)
  109. len = out;
  110. rc = len;
  111. if (out) {
  112. for (i = 0; i < entries; i++) {
  113. if (len < SG_BUFFER_SIZE)
  114. tocopy = len;
  115. else
  116. tocopy = SG_BUFFER_SIZE;
  117. memcpy(buf, vpd_buf[i], tocopy);
  118. buf += tocopy;
  119. len -= tocopy;
  120. }
  121. }
  122. }
  123. err2:
  124. for (i = 0; i < entries; i++) {
  125. if (vpd_buf[i])
  126. free_page((unsigned long) vpd_buf[i]);
  127. }
  128. free_page((unsigned long) le);
  129. err1:
  130. kfree(vpd_buf);
  131. return rc;
  132. }
  133. static int guest_get_irq_info(struct cxl_context *ctx, struct cxl_irq_info *info)
  134. {
  135. return cxl_h_collect_int_info(ctx->afu->guest->handle, ctx->process_token, info);
  136. }
  137. static irqreturn_t guest_psl_irq(int irq, void *data)
  138. {
  139. struct cxl_context *ctx = data;
  140. struct cxl_irq_info irq_info;
  141. int rc;
  142. pr_devel("%d: received PSL interrupt %i\n", ctx->pe, irq);
  143. rc = guest_get_irq_info(ctx, &irq_info);
  144. if (rc) {
  145. WARN(1, "Unable to get IRQ info: %i\n", rc);
  146. return IRQ_HANDLED;
  147. }
  148. rc = cxl_irq_psl8(irq, ctx, &irq_info);
  149. return rc;
  150. }
  151. static int afu_read_error_state(struct cxl_afu *afu, int *state_out)
  152. {
  153. u64 state;
  154. int rc = 0;
  155. if (!afu)
  156. return -EIO;
  157. rc = cxl_h_read_error_state(afu->guest->handle, &state);
  158. if (!rc) {
  159. WARN_ON(state != H_STATE_NORMAL &&
  160. state != H_STATE_DISABLE &&
  161. state != H_STATE_TEMP_UNAVAILABLE &&
  162. state != H_STATE_PERM_UNAVAILABLE);
  163. *state_out = state & 0xffffffff;
  164. }
  165. return rc;
  166. }
  167. static irqreturn_t guest_slice_irq_err(int irq, void *data)
  168. {
  169. struct cxl_afu *afu = data;
  170. int rc;
  171. u64 serr, afu_error, dsisr;
  172. rc = cxl_h_get_fn_error_interrupt(afu->guest->handle, &serr);
  173. if (rc) {
  174. dev_crit(&afu->dev, "Couldn't read PSL_SERR_An: %d\n", rc);
  175. return IRQ_HANDLED;
  176. }
  177. afu_error = cxl_p2n_read(afu, CXL_AFU_ERR_An);
  178. dsisr = cxl_p2n_read(afu, CXL_PSL_DSISR_An);
  179. cxl_afu_decode_psl_serr(afu, serr);
  180. dev_crit(&afu->dev, "AFU_ERR_An: 0x%.16llx\n", afu_error);
  181. dev_crit(&afu->dev, "PSL_DSISR_An: 0x%.16llx\n", dsisr);
  182. rc = cxl_h_ack_fn_error_interrupt(afu->guest->handle, serr);
  183. if (rc)
  184. dev_crit(&afu->dev, "Couldn't ack slice error interrupt: %d\n",
  185. rc);
  186. return IRQ_HANDLED;
  187. }
  188. static int irq_alloc_range(struct cxl *adapter, int len, int *irq)
  189. {
  190. int i, n;
  191. struct irq_avail *cur;
  192. for (i = 0; i < adapter->guest->irq_nranges; i++) {
  193. cur = &adapter->guest->irq_avail[i];
  194. n = bitmap_find_next_zero_area(cur->bitmap, cur->range,
  195. 0, len, 0);
  196. if (n < cur->range) {
  197. bitmap_set(cur->bitmap, n, len);
  198. *irq = cur->offset + n;
  199. pr_devel("guest: allocate IRQs %#x->%#x\n",
  200. *irq, *irq + len - 1);
  201. return 0;
  202. }
  203. }
  204. return -ENOSPC;
  205. }
  206. static int irq_free_range(struct cxl *adapter, int irq, int len)
  207. {
  208. int i, n;
  209. struct irq_avail *cur;
  210. if (len == 0)
  211. return -ENOENT;
  212. for (i = 0; i < adapter->guest->irq_nranges; i++) {
  213. cur = &adapter->guest->irq_avail[i];
  214. if (irq >= cur->offset &&
  215. (irq + len) <= (cur->offset + cur->range)) {
  216. n = irq - cur->offset;
  217. bitmap_clear(cur->bitmap, n, len);
  218. pr_devel("guest: release IRQs %#x->%#x\n",
  219. irq, irq + len - 1);
  220. return 0;
  221. }
  222. }
  223. return -ENOENT;
  224. }
  225. static int guest_reset(struct cxl *adapter)
  226. {
  227. struct cxl_afu *afu = NULL;
  228. int i, rc;
  229. pr_devel("Adapter reset request\n");
  230. spin_lock(&adapter->afu_list_lock);
  231. for (i = 0; i < adapter->slices; i++) {
  232. if ((afu = adapter->afu[i])) {
  233. pci_error_handlers(afu, CXL_ERROR_DETECTED_EVENT,
  234. pci_channel_io_frozen);
  235. cxl_context_detach_all(afu);
  236. }
  237. }
  238. rc = cxl_h_reset_adapter(adapter->guest->handle);
  239. for (i = 0; i < adapter->slices; i++) {
  240. if (!rc && (afu = adapter->afu[i])) {
  241. pci_error_handlers(afu, CXL_SLOT_RESET_EVENT,
  242. pci_channel_io_normal);
  243. pci_error_handlers(afu, CXL_RESUME_EVENT, 0);
  244. }
  245. }
  246. spin_unlock(&adapter->afu_list_lock);
  247. return rc;
  248. }
  249. static int guest_alloc_one_irq(struct cxl *adapter)
  250. {
  251. int irq;
  252. spin_lock(&adapter->guest->irq_alloc_lock);
  253. if (irq_alloc_range(adapter, 1, &irq))
  254. irq = -ENOSPC;
  255. spin_unlock(&adapter->guest->irq_alloc_lock);
  256. return irq;
  257. }
  258. static void guest_release_one_irq(struct cxl *adapter, int irq)
  259. {
  260. spin_lock(&adapter->guest->irq_alloc_lock);
  261. irq_free_range(adapter, irq, 1);
  262. spin_unlock(&adapter->guest->irq_alloc_lock);
  263. }
  264. static int guest_alloc_irq_ranges(struct cxl_irq_ranges *irqs,
  265. struct cxl *adapter, unsigned int num)
  266. {
  267. int i, try, irq;
  268. memset(irqs, 0, sizeof(struct cxl_irq_ranges));
  269. spin_lock(&adapter->guest->irq_alloc_lock);
  270. for (i = 0; i < CXL_IRQ_RANGES && num; i++) {
  271. try = num;
  272. while (try) {
  273. if (irq_alloc_range(adapter, try, &irq) == 0)
  274. break;
  275. try /= 2;
  276. }
  277. if (!try)
  278. goto error;
  279. irqs->offset[i] = irq;
  280. irqs->range[i] = try;
  281. num -= try;
  282. }
  283. if (num)
  284. goto error;
  285. spin_unlock(&adapter->guest->irq_alloc_lock);
  286. return 0;
  287. error:
  288. for (i = 0; i < CXL_IRQ_RANGES; i++)
  289. irq_free_range(adapter, irqs->offset[i], irqs->range[i]);
  290. spin_unlock(&adapter->guest->irq_alloc_lock);
  291. return -ENOSPC;
  292. }
  293. static void guest_release_irq_ranges(struct cxl_irq_ranges *irqs,
  294. struct cxl *adapter)
  295. {
  296. int i;
  297. spin_lock(&adapter->guest->irq_alloc_lock);
  298. for (i = 0; i < CXL_IRQ_RANGES; i++)
  299. irq_free_range(adapter, irqs->offset[i], irqs->range[i]);
  300. spin_unlock(&adapter->guest->irq_alloc_lock);
  301. }
  302. static int guest_register_serr_irq(struct cxl_afu *afu)
  303. {
  304. afu->err_irq_name = kasprintf(GFP_KERNEL, "cxl-%s-err",
  305. dev_name(&afu->dev));
  306. if (!afu->err_irq_name)
  307. return -ENOMEM;
  308. if (!(afu->serr_virq = cxl_map_irq(afu->adapter, afu->serr_hwirq,
  309. guest_slice_irq_err, afu, afu->err_irq_name))) {
  310. kfree(afu->err_irq_name);
  311. afu->err_irq_name = NULL;
  312. return -ENOMEM;
  313. }
  314. return 0;
  315. }
  316. static void guest_release_serr_irq(struct cxl_afu *afu)
  317. {
  318. cxl_unmap_irq(afu->serr_virq, afu);
  319. cxl_ops->release_one_irq(afu->adapter, afu->serr_hwirq);
  320. kfree(afu->err_irq_name);
  321. }
  322. static int guest_ack_irq(struct cxl_context *ctx, u64 tfc, u64 psl_reset_mask)
  323. {
  324. return cxl_h_control_faults(ctx->afu->guest->handle, ctx->process_token,
  325. tfc >> 32, (psl_reset_mask != 0));
  326. }
  327. static void disable_afu_irqs(struct cxl_context *ctx)
  328. {
  329. irq_hw_number_t hwirq;
  330. unsigned int virq;
  331. int r, i;
  332. pr_devel("Disabling AFU(%d) interrupts\n", ctx->afu->slice);
  333. for (r = 0; r < CXL_IRQ_RANGES; r++) {
  334. hwirq = ctx->irqs.offset[r];
  335. for (i = 0; i < ctx->irqs.range[r]; hwirq++, i++) {
  336. virq = irq_find_mapping(NULL, hwirq);
  337. disable_irq(virq);
  338. }
  339. }
  340. }
  341. static void enable_afu_irqs(struct cxl_context *ctx)
  342. {
  343. irq_hw_number_t hwirq;
  344. unsigned int virq;
  345. int r, i;
  346. pr_devel("Enabling AFU(%d) interrupts\n", ctx->afu->slice);
  347. for (r = 0; r < CXL_IRQ_RANGES; r++) {
  348. hwirq = ctx->irqs.offset[r];
  349. for (i = 0; i < ctx->irqs.range[r]; hwirq++, i++) {
  350. virq = irq_find_mapping(NULL, hwirq);
  351. enable_irq(virq);
  352. }
  353. }
  354. }
  355. static int _guest_afu_cr_readXX(int sz, struct cxl_afu *afu, int cr_idx,
  356. u64 offset, u64 *val)
  357. {
  358. unsigned long cr;
  359. char c;
  360. int rc = 0;
  361. if (afu->crs_len < sz)
  362. return -ENOENT;
  363. if (unlikely(offset >= afu->crs_len))
  364. return -ERANGE;
  365. cr = get_zeroed_page(GFP_KERNEL);
  366. if (!cr)
  367. return -ENOMEM;
  368. rc = cxl_h_get_config(afu->guest->handle, cr_idx, offset,
  369. virt_to_phys((void *)cr), sz);
  370. if (rc)
  371. goto err;
  372. switch (sz) {
  373. case 1:
  374. c = *((char *) cr);
  375. *val = c;
  376. break;
  377. case 2:
  378. *val = in_le16((u16 *)cr);
  379. break;
  380. case 4:
  381. *val = in_le32((unsigned *)cr);
  382. break;
  383. case 8:
  384. *val = in_le64((u64 *)cr);
  385. break;
  386. default:
  387. WARN_ON(1);
  388. }
  389. err:
  390. free_page(cr);
  391. return rc;
  392. }
  393. static int guest_afu_cr_read32(struct cxl_afu *afu, int cr_idx, u64 offset,
  394. u32 *out)
  395. {
  396. int rc;
  397. u64 val;
  398. rc = _guest_afu_cr_readXX(4, afu, cr_idx, offset, &val);
  399. if (!rc)
  400. *out = (u32) val;
  401. return rc;
  402. }
  403. static int guest_afu_cr_read16(struct cxl_afu *afu, int cr_idx, u64 offset,
  404. u16 *out)
  405. {
  406. int rc;
  407. u64 val;
  408. rc = _guest_afu_cr_readXX(2, afu, cr_idx, offset, &val);
  409. if (!rc)
  410. *out = (u16) val;
  411. return rc;
  412. }
  413. static int guest_afu_cr_read8(struct cxl_afu *afu, int cr_idx, u64 offset,
  414. u8 *out)
  415. {
  416. int rc;
  417. u64 val;
  418. rc = _guest_afu_cr_readXX(1, afu, cr_idx, offset, &val);
  419. if (!rc)
  420. *out = (u8) val;
  421. return rc;
  422. }
  423. static int guest_afu_cr_read64(struct cxl_afu *afu, int cr_idx, u64 offset,
  424. u64 *out)
  425. {
  426. return _guest_afu_cr_readXX(8, afu, cr_idx, offset, out);
  427. }
  428. static int guest_afu_cr_write32(struct cxl_afu *afu, int cr, u64 off, u32 in)
  429. {
  430. /* config record is not writable from guest */
  431. return -EPERM;
  432. }
  433. static int guest_afu_cr_write16(struct cxl_afu *afu, int cr, u64 off, u16 in)
  434. {
  435. /* config record is not writable from guest */
  436. return -EPERM;
  437. }
  438. static int guest_afu_cr_write8(struct cxl_afu *afu, int cr, u64 off, u8 in)
  439. {
  440. /* config record is not writable from guest */
  441. return -EPERM;
  442. }
  443. static int attach_afu_directed(struct cxl_context *ctx, u64 wed, u64 amr)
  444. {
  445. struct cxl_process_element_hcall *elem;
  446. struct cxl *adapter = ctx->afu->adapter;
  447. const struct cred *cred;
  448. u32 pid, idx;
  449. int rc, r, i;
  450. u64 mmio_addr, mmio_size;
  451. __be64 flags = 0;
  452. /* Must be 8 byte aligned and cannot cross a 4096 byte boundary */
  453. if (!(elem = (struct cxl_process_element_hcall *)
  454. get_zeroed_page(GFP_KERNEL)))
  455. return -ENOMEM;
  456. elem->version = cpu_to_be64(CXL_PROCESS_ELEMENT_VERSION);
  457. if (ctx->kernel) {
  458. pid = 0;
  459. flags |= CXL_PE_TRANSLATION_ENABLED;
  460. flags |= CXL_PE_PRIVILEGED_PROCESS;
  461. if (mfmsr() & MSR_SF)
  462. flags |= CXL_PE_64_BIT;
  463. } else {
  464. pid = current->pid;
  465. flags |= CXL_PE_PROBLEM_STATE;
  466. flags |= CXL_PE_TRANSLATION_ENABLED;
  467. if (!test_tsk_thread_flag(current, TIF_32BIT))
  468. flags |= CXL_PE_64_BIT;
  469. cred = get_current_cred();
  470. if (uid_eq(cred->euid, GLOBAL_ROOT_UID))
  471. flags |= CXL_PE_PRIVILEGED_PROCESS;
  472. put_cred(cred);
  473. }
  474. elem->flags = cpu_to_be64(flags);
  475. elem->common.tid = cpu_to_be32(0); /* Unused */
  476. elem->common.pid = cpu_to_be32(pid);
  477. elem->common.csrp = cpu_to_be64(0); /* disable */
  478. elem->common.u.psl8.aurp0 = cpu_to_be64(0); /* disable */
  479. elem->common.u.psl8.aurp1 = cpu_to_be64(0); /* disable */
  480. cxl_prefault(ctx, wed);
  481. elem->common.u.psl8.sstp0 = cpu_to_be64(ctx->sstp0);
  482. elem->common.u.psl8.sstp1 = cpu_to_be64(ctx->sstp1);
  483. /*
  484. * Ensure we have at least one interrupt allocated to take faults for
  485. * kernel contexts that may not have allocated any AFU IRQs at all:
  486. */
  487. if (ctx->irqs.range[0] == 0) {
  488. rc = afu_register_irqs(ctx, 0);
  489. if (rc)
  490. goto out_free;
  491. }
  492. for (r = 0; r < CXL_IRQ_RANGES; r++) {
  493. for (i = 0; i < ctx->irqs.range[r]; i++) {
  494. if (r == 0 && i == 0) {
  495. elem->pslVirtualIsn = cpu_to_be32(ctx->irqs.offset[0]);
  496. } else {
  497. idx = ctx->irqs.offset[r] + i - adapter->guest->irq_base_offset;
  498. elem->applicationVirtualIsnBitmap[idx / 8] |= 0x80 >> (idx % 8);
  499. }
  500. }
  501. }
  502. elem->common.amr = cpu_to_be64(amr);
  503. elem->common.wed = cpu_to_be64(wed);
  504. disable_afu_irqs(ctx);
  505. rc = cxl_h_attach_process(ctx->afu->guest->handle, elem,
  506. &ctx->process_token, &mmio_addr, &mmio_size);
  507. if (rc == H_SUCCESS) {
  508. if (ctx->master || !ctx->afu->pp_psa) {
  509. ctx->psn_phys = ctx->afu->psn_phys;
  510. ctx->psn_size = ctx->afu->adapter->ps_size;
  511. } else {
  512. ctx->psn_phys = mmio_addr;
  513. ctx->psn_size = mmio_size;
  514. }
  515. if (ctx->afu->pp_psa && mmio_size &&
  516. ctx->afu->pp_size == 0) {
  517. /*
  518. * There's no property in the device tree to read the
  519. * pp_size. We only find out at the 1st attach.
  520. * Compared to bare-metal, it is too late and we
  521. * should really lock here. However, on powerVM,
  522. * pp_size is really only used to display in /sys.
  523. * Being discussed with pHyp for their next release.
  524. */
  525. ctx->afu->pp_size = mmio_size;
  526. }
  527. /* from PAPR: process element is bytes 4-7 of process token */
  528. ctx->external_pe = ctx->process_token & 0xFFFFFFFF;
  529. pr_devel("CXL pe=%i is known as %i for pHyp, mmio_size=%#llx",
  530. ctx->pe, ctx->external_pe, ctx->psn_size);
  531. ctx->pe_inserted = true;
  532. enable_afu_irqs(ctx);
  533. }
  534. out_free:
  535. free_page((u64)elem);
  536. return rc;
  537. }
  538. static int guest_attach_process(struct cxl_context *ctx, bool kernel, u64 wed, u64 amr)
  539. {
  540. pr_devel("in %s\n", __func__);
  541. if (ctx->real_mode)
  542. return -EPERM;
  543. ctx->kernel = kernel;
  544. if (ctx->afu->current_mode == CXL_MODE_DIRECTED)
  545. return attach_afu_directed(ctx, wed, amr);
  546. /* dedicated mode not supported on FW840 */
  547. return -EINVAL;
  548. }
  549. static int detach_afu_directed(struct cxl_context *ctx)
  550. {
  551. if (!ctx->pe_inserted)
  552. return 0;
  553. if (cxl_h_detach_process(ctx->afu->guest->handle, ctx->process_token))
  554. return -1;
  555. return 0;
  556. }
  557. static int guest_detach_process(struct cxl_context *ctx)
  558. {
  559. pr_devel("in %s\n", __func__);
  560. trace_cxl_detach(ctx);
  561. if (!cxl_ops->link_ok(ctx->afu->adapter, ctx->afu))
  562. return -EIO;
  563. if (ctx->afu->current_mode == CXL_MODE_DIRECTED)
  564. return detach_afu_directed(ctx);
  565. return -EINVAL;
  566. }
  567. static void guest_release_afu(struct device *dev)
  568. {
  569. struct cxl_afu *afu = to_cxl_afu(dev);
  570. pr_devel("%s\n", __func__);
  571. idr_destroy(&afu->contexts_idr);
  572. kfree(afu->guest);
  573. kfree(afu);
  574. }
  575. ssize_t cxl_guest_read_afu_vpd(struct cxl_afu *afu, void *buf, size_t len)
  576. {
  577. return guest_collect_vpd(NULL, afu, buf, len);
  578. }
  579. #define ERR_BUFF_MAX_COPY_SIZE PAGE_SIZE
  580. static ssize_t guest_afu_read_err_buffer(struct cxl_afu *afu, char *buf,
  581. loff_t off, size_t count)
  582. {
  583. void *tbuf = NULL;
  584. int rc = 0;
  585. tbuf = (void *) get_zeroed_page(GFP_KERNEL);
  586. if (!tbuf)
  587. return -ENOMEM;
  588. rc = cxl_h_get_afu_err(afu->guest->handle,
  589. off & 0x7,
  590. virt_to_phys(tbuf),
  591. count);
  592. if (rc)
  593. goto err;
  594. if (count > ERR_BUFF_MAX_COPY_SIZE)
  595. count = ERR_BUFF_MAX_COPY_SIZE - (off & 0x7);
  596. memcpy(buf, tbuf, count);
  597. err:
  598. free_page((u64)tbuf);
  599. return rc;
  600. }
  601. static int guest_afu_check_and_enable(struct cxl_afu *afu)
  602. {
  603. return 0;
  604. }
  605. static bool guest_support_attributes(const char *attr_name,
  606. enum cxl_attrs type)
  607. {
  608. switch (type) {
  609. case CXL_ADAPTER_ATTRS:
  610. if ((strcmp(attr_name, "base_image") == 0) ||
  611. (strcmp(attr_name, "load_image_on_perst") == 0) ||
  612. (strcmp(attr_name, "perst_reloads_same_image") == 0) ||
  613. (strcmp(attr_name, "image_loaded") == 0))
  614. return false;
  615. break;
  616. case CXL_AFU_MASTER_ATTRS:
  617. if ((strcmp(attr_name, "pp_mmio_off") == 0))
  618. return false;
  619. break;
  620. case CXL_AFU_ATTRS:
  621. break;
  622. default:
  623. break;
  624. }
  625. return true;
  626. }
  627. static int activate_afu_directed(struct cxl_afu *afu)
  628. {
  629. int rc;
  630. dev_info(&afu->dev, "Activating AFU(%d) directed mode\n", afu->slice);
  631. afu->current_mode = CXL_MODE_DIRECTED;
  632. afu->num_procs = afu->max_procs_virtualised;
  633. if ((rc = cxl_chardev_m_afu_add(afu)))
  634. return rc;
  635. if ((rc = cxl_sysfs_afu_m_add(afu)))
  636. goto err;
  637. if ((rc = cxl_chardev_s_afu_add(afu)))
  638. goto err1;
  639. return 0;
  640. err1:
  641. cxl_sysfs_afu_m_remove(afu);
  642. err:
  643. cxl_chardev_afu_remove(afu);
  644. return rc;
  645. }
  646. static int guest_afu_activate_mode(struct cxl_afu *afu, int mode)
  647. {
  648. if (!mode)
  649. return 0;
  650. if (!(mode & afu->modes_supported))
  651. return -EINVAL;
  652. if (mode == CXL_MODE_DIRECTED)
  653. return activate_afu_directed(afu);
  654. if (mode == CXL_MODE_DEDICATED)
  655. dev_err(&afu->dev, "Dedicated mode not supported\n");
  656. return -EINVAL;
  657. }
  658. static int deactivate_afu_directed(struct cxl_afu *afu)
  659. {
  660. dev_info(&afu->dev, "Deactivating AFU(%d) directed mode\n", afu->slice);
  661. afu->current_mode = 0;
  662. afu->num_procs = 0;
  663. cxl_sysfs_afu_m_remove(afu);
  664. cxl_chardev_afu_remove(afu);
  665. cxl_ops->afu_reset(afu);
  666. return 0;
  667. }
  668. static int guest_afu_deactivate_mode(struct cxl_afu *afu, int mode)
  669. {
  670. if (!mode)
  671. return 0;
  672. if (!(mode & afu->modes_supported))
  673. return -EINVAL;
  674. if (mode == CXL_MODE_DIRECTED)
  675. return deactivate_afu_directed(afu);
  676. return 0;
  677. }
  678. static int guest_afu_reset(struct cxl_afu *afu)
  679. {
  680. pr_devel("AFU(%d) reset request\n", afu->slice);
  681. return cxl_h_reset_afu(afu->guest->handle);
  682. }
  683. static int guest_map_slice_regs(struct cxl_afu *afu)
  684. {
  685. if (!(afu->p2n_mmio = ioremap(afu->guest->p2n_phys, afu->guest->p2n_size))) {
  686. dev_err(&afu->dev, "Error mapping AFU(%d) MMIO regions\n",
  687. afu->slice);
  688. return -ENOMEM;
  689. }
  690. return 0;
  691. }
  692. static void guest_unmap_slice_regs(struct cxl_afu *afu)
  693. {
  694. if (afu->p2n_mmio)
  695. iounmap(afu->p2n_mmio);
  696. }
  697. static int afu_update_state(struct cxl_afu *afu)
  698. {
  699. int rc, cur_state;
  700. rc = afu_read_error_state(afu, &cur_state);
  701. if (rc)
  702. return rc;
  703. if (afu->guest->previous_state == cur_state)
  704. return 0;
  705. pr_devel("AFU(%d) update state to %#x\n", afu->slice, cur_state);
  706. switch (cur_state) {
  707. case H_STATE_NORMAL:
  708. afu->guest->previous_state = cur_state;
  709. break;
  710. case H_STATE_DISABLE:
  711. pci_error_handlers(afu, CXL_ERROR_DETECTED_EVENT,
  712. pci_channel_io_frozen);
  713. cxl_context_detach_all(afu);
  714. if ((rc = cxl_ops->afu_reset(afu)))
  715. pr_devel("reset hcall failed %d\n", rc);
  716. rc = afu_read_error_state(afu, &cur_state);
  717. if (!rc && cur_state == H_STATE_NORMAL) {
  718. pci_error_handlers(afu, CXL_SLOT_RESET_EVENT,
  719. pci_channel_io_normal);
  720. pci_error_handlers(afu, CXL_RESUME_EVENT, 0);
  721. }
  722. afu->guest->previous_state = 0;
  723. break;
  724. case H_STATE_TEMP_UNAVAILABLE:
  725. afu->guest->previous_state = cur_state;
  726. break;
  727. case H_STATE_PERM_UNAVAILABLE:
  728. dev_err(&afu->dev, "AFU is in permanent error state\n");
  729. pci_error_handlers(afu, CXL_ERROR_DETECTED_EVENT,
  730. pci_channel_io_perm_failure);
  731. afu->guest->previous_state = cur_state;
  732. break;
  733. default:
  734. pr_err("Unexpected AFU(%d) error state: %#x\n",
  735. afu->slice, cur_state);
  736. return -EINVAL;
  737. }
  738. return rc;
  739. }
  740. static void afu_handle_errstate(struct work_struct *work)
  741. {
  742. struct cxl_afu_guest *afu_guest =
  743. container_of(to_delayed_work(work), struct cxl_afu_guest, work_err);
  744. if (!afu_update_state(afu_guest->parent) &&
  745. afu_guest->previous_state == H_STATE_PERM_UNAVAILABLE)
  746. return;
  747. if (afu_guest->handle_err)
  748. schedule_delayed_work(&afu_guest->work_err,
  749. msecs_to_jiffies(3000));
  750. }
  751. static bool guest_link_ok(struct cxl *cxl, struct cxl_afu *afu)
  752. {
  753. int state;
  754. if (afu && (!afu_read_error_state(afu, &state))) {
  755. if (state == H_STATE_NORMAL)
  756. return true;
  757. }
  758. return false;
  759. }
  760. static int afu_properties_look_ok(struct cxl_afu *afu)
  761. {
  762. if (afu->pp_irqs < 0) {
  763. dev_err(&afu->dev, "Unexpected per-process minimum interrupt value\n");
  764. return -EINVAL;
  765. }
  766. if (afu->max_procs_virtualised < 1) {
  767. dev_err(&afu->dev, "Unexpected max number of processes virtualised value\n");
  768. return -EINVAL;
  769. }
  770. if (afu->crs_len < 0) {
  771. dev_err(&afu->dev, "Unexpected configuration record size value\n");
  772. return -EINVAL;
  773. }
  774. return 0;
  775. }
  776. int cxl_guest_init_afu(struct cxl *adapter, int slice, struct device_node *afu_np)
  777. {
  778. struct cxl_afu *afu;
  779. bool free = true;
  780. int rc;
  781. pr_devel("in %s - AFU(%d)\n", __func__, slice);
  782. if (!(afu = cxl_alloc_afu(adapter, slice)))
  783. return -ENOMEM;
  784. if (!(afu->guest = kzalloc(sizeof(struct cxl_afu_guest), GFP_KERNEL))) {
  785. kfree(afu);
  786. return -ENOMEM;
  787. }
  788. if ((rc = dev_set_name(&afu->dev, "afu%i.%i",
  789. adapter->adapter_num,
  790. slice)))
  791. goto err1;
  792. adapter->slices++;
  793. if ((rc = cxl_of_read_afu_handle(afu, afu_np)))
  794. goto err1;
  795. if ((rc = cxl_ops->afu_reset(afu)))
  796. goto err1;
  797. if ((rc = cxl_of_read_afu_properties(afu, afu_np)))
  798. goto err1;
  799. if ((rc = afu_properties_look_ok(afu)))
  800. goto err1;
  801. if ((rc = guest_map_slice_regs(afu)))
  802. goto err1;
  803. if ((rc = guest_register_serr_irq(afu)))
  804. goto err2;
  805. /*
  806. * After we call this function we must not free the afu directly, even
  807. * if it returns an error!
  808. */
  809. if ((rc = cxl_register_afu(afu)))
  810. goto err_put1;
  811. if ((rc = cxl_sysfs_afu_add(afu)))
  812. goto err_put1;
  813. /*
  814. * pHyp doesn't expose the programming models supported by the
  815. * AFU. pHyp currently only supports directed mode. If it adds
  816. * dedicated mode later, this version of cxl has no way to
  817. * detect it. So we'll initialize the driver, but the first
  818. * attach will fail.
  819. * Being discussed with pHyp to do better (likely new property)
  820. */
  821. if (afu->max_procs_virtualised == 1)
  822. afu->modes_supported = CXL_MODE_DEDICATED;
  823. else
  824. afu->modes_supported = CXL_MODE_DIRECTED;
  825. if ((rc = cxl_afu_select_best_mode(afu)))
  826. goto err_put2;
  827. adapter->afu[afu->slice] = afu;
  828. afu->enabled = true;
  829. /*
  830. * wake up the cpu periodically to check the state
  831. * of the AFU using "afu" stored in the guest structure.
  832. */
  833. afu->guest->parent = afu;
  834. afu->guest->handle_err = true;
  835. INIT_DELAYED_WORK(&afu->guest->work_err, afu_handle_errstate);
  836. schedule_delayed_work(&afu->guest->work_err, msecs_to_jiffies(1000));
  837. if ((rc = cxl_pci_vphb_add(afu)))
  838. dev_info(&afu->dev, "Can't register vPHB\n");
  839. return 0;
  840. err_put2:
  841. cxl_sysfs_afu_remove(afu);
  842. err_put1:
  843. device_unregister(&afu->dev);
  844. free = false;
  845. guest_release_serr_irq(afu);
  846. err2:
  847. guest_unmap_slice_regs(afu);
  848. err1:
  849. if (free) {
  850. kfree(afu->guest);
  851. kfree(afu);
  852. }
  853. return rc;
  854. }
  855. void cxl_guest_remove_afu(struct cxl_afu *afu)
  856. {
  857. if (!afu)
  858. return;
  859. /* flush and stop pending job */
  860. afu->guest->handle_err = false;
  861. flush_delayed_work(&afu->guest->work_err);
  862. cxl_pci_vphb_remove(afu);
  863. cxl_sysfs_afu_remove(afu);
  864. spin_lock(&afu->adapter->afu_list_lock);
  865. afu->adapter->afu[afu->slice] = NULL;
  866. spin_unlock(&afu->adapter->afu_list_lock);
  867. cxl_context_detach_all(afu);
  868. cxl_ops->afu_deactivate_mode(afu, afu->current_mode);
  869. guest_release_serr_irq(afu);
  870. guest_unmap_slice_regs(afu);
  871. device_unregister(&afu->dev);
  872. }
  873. static void free_adapter(struct cxl *adapter)
  874. {
  875. struct irq_avail *cur;
  876. int i;
  877. if (adapter->guest) {
  878. if (adapter->guest->irq_avail) {
  879. for (i = 0; i < adapter->guest->irq_nranges; i++) {
  880. cur = &adapter->guest->irq_avail[i];
  881. kfree(cur->bitmap);
  882. }
  883. kfree(adapter->guest->irq_avail);
  884. }
  885. kfree(adapter->guest->status);
  886. kfree(adapter->guest);
  887. }
  888. cxl_remove_adapter_nr(adapter);
  889. kfree(adapter);
  890. }
  891. static int properties_look_ok(struct cxl *adapter)
  892. {
  893. /* The absence of this property means that the operational
  894. * status is unknown or okay
  895. */
  896. if (strlen(adapter->guest->status) &&
  897. strcmp(adapter->guest->status, "okay")) {
  898. pr_err("ABORTING:Bad operational status of the device\n");
  899. return -EINVAL;
  900. }
  901. return 0;
  902. }
  903. ssize_t cxl_guest_read_adapter_vpd(struct cxl *adapter, void *buf, size_t len)
  904. {
  905. return guest_collect_vpd(adapter, NULL, buf, len);
  906. }
  907. void cxl_guest_remove_adapter(struct cxl *adapter)
  908. {
  909. pr_devel("in %s\n", __func__);
  910. cxl_sysfs_adapter_remove(adapter);
  911. cxl_guest_remove_chardev(adapter);
  912. device_unregister(&adapter->dev);
  913. }
  914. static void release_adapter(struct device *dev)
  915. {
  916. free_adapter(to_cxl_adapter(dev));
  917. }
  918. struct cxl *cxl_guest_init_adapter(struct device_node *np, struct platform_device *pdev)
  919. {
  920. struct cxl *adapter;
  921. bool free = true;
  922. int rc;
  923. if (!(adapter = cxl_alloc_adapter()))
  924. return ERR_PTR(-ENOMEM);
  925. if (!(adapter->guest = kzalloc(sizeof(struct cxl_guest), GFP_KERNEL))) {
  926. free_adapter(adapter);
  927. return ERR_PTR(-ENOMEM);
  928. }
  929. adapter->slices = 0;
  930. adapter->guest->pdev = pdev;
  931. adapter->dev.parent = &pdev->dev;
  932. adapter->dev.release = release_adapter;
  933. dev_set_drvdata(&pdev->dev, adapter);
  934. /*
  935. * Hypervisor controls PSL timebase initialization (p1 register).
  936. * On FW840, PSL is initialized.
  937. */
  938. adapter->psl_timebase_synced = true;
  939. if ((rc = cxl_of_read_adapter_handle(adapter, np)))
  940. goto err1;
  941. if ((rc = cxl_of_read_adapter_properties(adapter, np)))
  942. goto err1;
  943. if ((rc = properties_look_ok(adapter)))
  944. goto err1;
  945. if ((rc = cxl_guest_add_chardev(adapter)))
  946. goto err1;
  947. /*
  948. * After we call this function we must not free the adapter directly,
  949. * even if it returns an error!
  950. */
  951. if ((rc = cxl_register_adapter(adapter)))
  952. goto err_put1;
  953. if ((rc = cxl_sysfs_adapter_add(adapter)))
  954. goto err_put1;
  955. /* release the context lock as the adapter is configured */
  956. cxl_adapter_context_unlock(adapter);
  957. return adapter;
  958. err_put1:
  959. device_unregister(&adapter->dev);
  960. free = false;
  961. cxl_guest_remove_chardev(adapter);
  962. err1:
  963. if (free)
  964. free_adapter(adapter);
  965. return ERR_PTR(rc);
  966. }
  967. void cxl_guest_reload_module(struct cxl *adapter)
  968. {
  969. struct platform_device *pdev;
  970. pdev = adapter->guest->pdev;
  971. cxl_guest_remove_adapter(adapter);
  972. cxl_of_probe(pdev);
  973. }
  974. const struct cxl_backend_ops cxl_guest_ops = {
  975. .module = THIS_MODULE,
  976. .adapter_reset = guest_reset,
  977. .alloc_one_irq = guest_alloc_one_irq,
  978. .release_one_irq = guest_release_one_irq,
  979. .alloc_irq_ranges = guest_alloc_irq_ranges,
  980. .release_irq_ranges = guest_release_irq_ranges,
  981. .setup_irq = NULL,
  982. .handle_psl_slice_error = guest_handle_psl_slice_error,
  983. .psl_interrupt = guest_psl_irq,
  984. .ack_irq = guest_ack_irq,
  985. .attach_process = guest_attach_process,
  986. .detach_process = guest_detach_process,
  987. .update_ivtes = NULL,
  988. .support_attributes = guest_support_attributes,
  989. .link_ok = guest_link_ok,
  990. .release_afu = guest_release_afu,
  991. .afu_read_err_buffer = guest_afu_read_err_buffer,
  992. .afu_check_and_enable = guest_afu_check_and_enable,
  993. .afu_activate_mode = guest_afu_activate_mode,
  994. .afu_deactivate_mode = guest_afu_deactivate_mode,
  995. .afu_reset = guest_afu_reset,
  996. .afu_cr_read8 = guest_afu_cr_read8,
  997. .afu_cr_read16 = guest_afu_cr_read16,
  998. .afu_cr_read32 = guest_afu_cr_read32,
  999. .afu_cr_read64 = guest_afu_cr_read64,
  1000. .afu_cr_write8 = guest_afu_cr_write8,
  1001. .afu_cr_write16 = guest_afu_cr_write16,
  1002. .afu_cr_write32 = guest_afu_cr_write32,
  1003. .read_adapter_vpd = cxl_guest_read_adapter_vpd,
  1004. };