1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Driver for IBM PowerNV compression accelerator
5 * Copyright (C) 2015 Dan Streetman, IBM Corp
8 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
12 #include <linux/timer.h>
15 #include <asm/icswx.h>
18 #include <asm/opal-api.h>
21 MODULE_LICENSE("GPL");
22 MODULE_AUTHOR("Dan Streetman <ddstreet@ieee.org>");
23 MODULE_DESCRIPTION("H/W Compression driver for IBM PowerNV processors");
24 MODULE_ALIAS_CRYPTO("842");
25 MODULE_ALIAS_CRYPTO("842-nx");
27 #define WORKMEM_ALIGN (CRB_ALIGN)
28 #define CSB_WAIT_MAX (5000) /* ms */
29 #define VAS_RETRIES (10)
31 struct nx842_workmem
{
32 /* Below fields must be properly aligned */
33 struct coprocessor_request_block crb
; /* CRB_ALIGN align */
34 struct data_descriptor_entry ddl_in
[DDL_LEN_MAX
]; /* DDE_ALIGN align */
35 struct data_descriptor_entry ddl_out
[DDL_LEN_MAX
]; /* DDE_ALIGN align */
36 /* Above fields must be properly aligned */
40 char padding
[WORKMEM_ALIGN
]; /* unused, to allow alignment */
41 } __packed
__aligned(WORKMEM_ALIGN
);
45 unsigned int ct
; /* Can be 842 or GZIP high/normal*/
46 unsigned int ci
; /* Coprocessor instance, used with icswx */
48 struct vas_window
*rxwin
;
51 struct list_head list
;
55 * Send the request to NX engine on the chip for the corresponding CPU
56 * where the process is executing. Use with VAS function.
58 static DEFINE_PER_CPU(struct vas_window
*, cpu_txwin
);
60 /* no cpu hotplug on powernv, so this list never changes after init */
61 static LIST_HEAD(nx_coprocs
);
62 static unsigned int nx842_ct
; /* used in icswx function */
65 * Using same values as in skiboot or coprocessor type representing
68 #define NX_CT_GZIP (2) /* on P9 and later */
71 static int (*nx842_powernv_exec
)(const unsigned char *in
,
72 unsigned int inlen
, unsigned char *out
,
73 unsigned int *outlenp
, void *workmem
, int fc
);
76 * setup_indirect_dde - Setup an indirect DDE
78 * The DDE is setup with the the DDE count, byte count, and address of
79 * first direct DDE in the list.
81 static void setup_indirect_dde(struct data_descriptor_entry
*dde
,
82 struct data_descriptor_entry
*ddl
,
83 unsigned int dde_count
, unsigned int byte_count
)
86 dde
->count
= dde_count
;
88 dde
->length
= cpu_to_be32(byte_count
);
89 dde
->address
= cpu_to_be64(nx842_get_pa(ddl
));
93 * setup_direct_dde - Setup single DDE from buffer
95 * The DDE is setup with the buffer and length. The buffer must be properly
96 * aligned. The used length is returned.
98 * N Successfully set up DDE with N bytes
100 static unsigned int setup_direct_dde(struct data_descriptor_entry
*dde
,
101 unsigned long pa
, unsigned int len
)
103 unsigned int l
= min_t(unsigned int, len
, LEN_ON_PAGE(pa
));
108 dde
->length
= cpu_to_be32(l
);
109 dde
->address
= cpu_to_be64(pa
);
115 * setup_ddl - Setup DDL from buffer
118 * 0 Successfully set up DDL
120 static int setup_ddl(struct data_descriptor_entry
*dde
,
121 struct data_descriptor_entry
*ddl
,
122 unsigned char *buf
, unsigned int len
,
125 unsigned long pa
= nx842_get_pa(buf
);
126 int i
, ret
, total_len
= len
;
128 if (!IS_ALIGNED(pa
, DDE_BUFFER_ALIGN
)) {
129 pr_debug("%s buffer pa 0x%lx not 0x%x-byte aligned\n",
130 in
? "input" : "output", pa
, DDE_BUFFER_ALIGN
);
134 /* only need to check last mult; since buffer must be
135 * DDE_BUFFER_ALIGN aligned, and that is a multiple of
136 * DDE_BUFFER_SIZE_MULT, and pre-last page DDE buffers
137 * are guaranteed a multiple of DDE_BUFFER_SIZE_MULT.
139 if (len
% DDE_BUFFER_LAST_MULT
) {
140 pr_debug("%s buffer len 0x%x not a multiple of 0x%x\n",
141 in
? "input" : "output", len
, DDE_BUFFER_LAST_MULT
);
144 len
= round_down(len
, DDE_BUFFER_LAST_MULT
);
147 /* use a single direct DDE */
148 if (len
<= LEN_ON_PAGE(pa
)) {
149 ret
= setup_direct_dde(dde
, pa
, len
);
155 for (i
= 0; i
< DDL_LEN_MAX
&& len
> 0; i
++) {
156 ret
= setup_direct_dde(&ddl
[i
], pa
, len
);
159 pa
= nx842_get_pa(buf
);
163 pr_debug("0x%x total %s bytes 0x%x too many for DDL.\n",
164 total_len
, in
? "input" : "output", len
);
169 setup_indirect_dde(dde
, ddl
, i
, total_len
);
174 #define CSB_ERR(csb, msg, ...) \
175 pr_err("ERROR: " msg " : %02x %02x %02x %02x %08x\n", \
176 ##__VA_ARGS__, (csb)->flags, \
177 (csb)->cs, (csb)->cc, (csb)->ce, \
178 be32_to_cpu((csb)->count))
180 #define CSB_ERR_ADDR(csb, msg, ...) \
181 CSB_ERR(csb, msg " at %lx", ##__VA_ARGS__, \
182 (unsigned long)be64_to_cpu((csb)->address))
187 static int wait_for_csb(struct nx842_workmem
*wmem
,
188 struct coprocessor_status_block
*csb
)
190 ktime_t start
= wmem
->start
, now
= ktime_get();
191 ktime_t timeout
= ktime_add_ms(start
, CSB_WAIT_MAX
);
193 while (!(READ_ONCE(csb
->flags
) & CSB_V
)) {
196 if (ktime_after(now
, timeout
))
200 /* hw has updated csb and output buffer */
203 /* check CSB flags */
204 if (!(csb
->flags
& CSB_V
)) {
205 CSB_ERR(csb
, "CSB still not valid after %ld us, giving up",
206 (long)ktime_us_delta(now
, start
));
209 if (csb
->flags
& CSB_F
) {
210 CSB_ERR(csb
, "Invalid CSB format");
213 if (csb
->flags
& CSB_CH
) {
214 CSB_ERR(csb
, "Invalid CSB chaining state");
218 /* verify CSB completion sequence is 0 */
220 CSB_ERR(csb
, "Invalid CSB completion sequence");
224 /* check CSB Completion Code */
229 case CSB_CC_TPBC_GT_SPBC
:
230 /* not an error, but the compressed data is
231 * larger than the uncompressed data :(
235 /* input data errors */
236 case CSB_CC_OPERAND_OVERLAP
:
237 /* input and output buffers overlap */
238 CSB_ERR(csb
, "Operand Overlap error");
240 case CSB_CC_INVALID_OPERAND
:
241 CSB_ERR(csb
, "Invalid operand");
244 /* output buffer too small */
247 CSB_ERR(csb
, "Function aborted");
249 case CSB_CC_CRC_MISMATCH
:
250 CSB_ERR(csb
, "CRC mismatch");
252 case CSB_CC_TEMPL_INVALID
:
253 CSB_ERR(csb
, "Compressed data template invalid");
255 case CSB_CC_TEMPL_OVERFLOW
:
256 CSB_ERR(csb
, "Compressed data template shows data past end");
258 case CSB_CC_EXCEED_BYTE_COUNT
: /* P9 or later */
260 * DDE byte count exceeds the limit specified in Maximum
261 * byte count register.
263 CSB_ERR(csb
, "DDE byte count exceeds the limit");
266 /* these should not happen */
267 case CSB_CC_INVALID_ALIGN
:
268 /* setup_ddl should have detected this */
269 CSB_ERR_ADDR(csb
, "Invalid alignment");
271 case CSB_CC_DATA_LENGTH
:
272 /* setup_ddl should have detected this */
273 CSB_ERR(csb
, "Invalid data length");
275 case CSB_CC_WR_TRANSLATION
:
276 case CSB_CC_TRANSLATION
:
277 case CSB_CC_TRANSLATION_DUP1
:
278 case CSB_CC_TRANSLATION_DUP2
:
279 case CSB_CC_TRANSLATION_DUP3
:
280 case CSB_CC_TRANSLATION_DUP4
:
281 case CSB_CC_TRANSLATION_DUP5
:
282 case CSB_CC_TRANSLATION_DUP6
:
283 /* should not happen, we use physical addrs */
284 CSB_ERR_ADDR(csb
, "Translation error");
286 case CSB_CC_WR_PROTECTION
:
287 case CSB_CC_PROTECTION
:
288 case CSB_CC_PROTECTION_DUP1
:
289 case CSB_CC_PROTECTION_DUP2
:
290 case CSB_CC_PROTECTION_DUP3
:
291 case CSB_CC_PROTECTION_DUP4
:
292 case CSB_CC_PROTECTION_DUP5
:
293 case CSB_CC_PROTECTION_DUP6
:
294 /* should not happen, we use physical addrs */
295 CSB_ERR_ADDR(csb
, "Protection error");
297 case CSB_CC_PRIVILEGE
:
298 /* shouldn't happen, we're in HYP mode */
299 CSB_ERR(csb
, "Insufficient Privilege error");
301 case CSB_CC_EXCESSIVE_DDE
:
302 /* shouldn't happen, setup_ddl doesn't use many dde's */
303 CSB_ERR(csb
, "Too many DDEs in DDL");
305 case CSB_CC_TRANSPORT
:
306 case CSB_CC_INVALID_CRB
: /* P9 or later */
307 /* shouldn't happen, we setup CRB correctly */
308 CSB_ERR(csb
, "Invalid CRB");
310 case CSB_CC_INVALID_DDE
: /* P9 or later */
312 * shouldn't happen, setup_direct/indirect_dde creates
315 CSB_ERR(csb
, "Invalid DDE");
317 case CSB_CC_SEGMENTED_DDL
:
318 /* shouldn't happen, setup_ddl creates DDL right */
319 CSB_ERR(csb
, "Segmented DDL error");
321 case CSB_CC_DDE_OVERFLOW
:
322 /* shouldn't happen, setup_ddl creates DDL right */
323 CSB_ERR(csb
, "DDE overflow error");
326 /* should not happen with ICSWX */
327 CSB_ERR(csb
, "Session violation error");
330 /* should not happen, we don't use chained CRBs */
331 CSB_ERR(csb
, "Chained CRB error");
333 case CSB_CC_SEQUENCE
:
334 /* should not happen, we don't use chained CRBs */
335 CSB_ERR(csb
, "CRB sequence number error");
337 case CSB_CC_UNKNOWN_CODE
:
338 CSB_ERR(csb
, "Unknown subfunction code");
341 /* hardware errors */
342 case CSB_CC_RD_EXTERNAL
:
343 case CSB_CC_RD_EXTERNAL_DUP1
:
344 case CSB_CC_RD_EXTERNAL_DUP2
:
345 case CSB_CC_RD_EXTERNAL_DUP3
:
346 CSB_ERR_ADDR(csb
, "Read error outside coprocessor");
348 case CSB_CC_WR_EXTERNAL
:
349 CSB_ERR_ADDR(csb
, "Write error outside coprocessor");
351 case CSB_CC_INTERNAL
:
352 CSB_ERR(csb
, "Internal error in coprocessor");
354 case CSB_CC_PROVISION
:
355 CSB_ERR(csb
, "Storage provision error");
358 CSB_ERR(csb
, "Correctable hardware error");
360 case CSB_CC_HW_EXPIRED_TIMER
: /* P9 or later */
361 CSB_ERR(csb
, "Job did not finish within allowed time");
365 CSB_ERR(csb
, "Invalid CC %d", csb
->cc
);
369 /* check Completion Extension state */
370 if (csb
->ce
& CSB_CE_TERMINATION
) {
371 CSB_ERR(csb
, "CSB request was terminated");
374 if (csb
->ce
& CSB_CE_INCOMPLETE
) {
375 CSB_ERR(csb
, "CSB request not complete");
378 if (!(csb
->ce
& CSB_CE_TPBC
)) {
379 CSB_ERR(csb
, "TPBC not provided, unknown target length");
383 /* successful completion */
384 pr_debug_ratelimited("Processed %u bytes in %lu us\n",
385 be32_to_cpu(csb
->count
),
386 (unsigned long)ktime_us_delta(now
, start
));
391 static int nx842_config_crb(const unsigned char *in
, unsigned int inlen
,
392 unsigned char *out
, unsigned int outlen
,
393 struct nx842_workmem
*wmem
)
395 struct coprocessor_request_block
*crb
;
396 struct coprocessor_status_block
*csb
;
403 /* Clear any previous values */
404 memset(crb
, 0, sizeof(*crb
));
407 ret
= setup_ddl(&crb
->source
, wmem
->ddl_in
,
408 (unsigned char *)in
, inlen
, true);
412 ret
= setup_ddl(&crb
->target
, wmem
->ddl_out
,
417 /* set up CRB's CSB addr */
418 csb_addr
= nx842_get_pa(csb
) & CRB_CSB_ADDRESS
;
419 csb_addr
|= CRB_CSB_AT
; /* Addrs are phys */
420 crb
->csb_addr
= cpu_to_be64(csb_addr
);
426 * nx842_exec_icswx - compress/decompress data using the 842 algorithm
428 * (De)compression provided by the NX842 coprocessor on IBM PowerNV systems.
429 * This compresses or decompresses the provided input buffer into the provided
432 * Upon return from this function @outlen contains the length of the
433 * output data. If there is an error then @outlen will be 0 and an
434 * error will be specified by the return code from this function.
436 * The @workmem buffer should only be used by one function call at a time.
438 * @in: input buffer pointer
439 * @inlen: input buffer size
440 * @out: output buffer pointer
441 * @outlenp: output buffer size pointer
442 * @workmem: working memory buffer pointer, size determined by
443 * nx842_powernv_driver.workmem_size
444 * @fc: function code, see CCW Function Codes in nx-842.h
447 * 0 Success, output of length @outlenp stored in the buffer at @out
448 * -ENODEV Hardware unavailable
449 * -ENOSPC Output buffer is to small
450 * -EMSGSIZE Input buffer too large
451 * -EINVAL buffer constraints do not fix nx842_constraints
452 * -EPROTO hardware error during operation
453 * -ETIMEDOUT hardware did not complete operation in reasonable time
454 * -EINTR operation was aborted
456 static int nx842_exec_icswx(const unsigned char *in
, unsigned int inlen
,
457 unsigned char *out
, unsigned int *outlenp
,
458 void *workmem
, int fc
)
460 struct coprocessor_request_block
*crb
;
461 struct coprocessor_status_block
*csb
;
462 struct nx842_workmem
*wmem
;
465 unsigned int outlen
= *outlenp
;
467 wmem
= PTR_ALIGN(workmem
, WORKMEM_ALIGN
);
471 /* shoudn't happen, we don't load without a coproc */
473 pr_err_ratelimited("coprocessor CT is 0");
477 ret
= nx842_config_crb(in
, inlen
, out
, outlen
, wmem
);
486 ccw
= SET_FIELD(CCW_CT
, ccw
, nx842_ct
);
487 ccw
= SET_FIELD(CCW_CI_842
, ccw
, 0); /* use 0 for hw auto-selection */
488 ccw
= SET_FIELD(CCW_FC_842
, ccw
, fc
);
490 wmem
->start
= ktime_get();
493 ret
= icswx(cpu_to_be32(ccw
), crb
);
495 pr_debug_ratelimited("icswx CR %x ccw %x crb->ccw %x\n", ret
,
497 (unsigned int)be32_to_cpu(crb
->ccw
));
500 * NX842 coprocessor sets 3rd bit in CR register with XER[S0].
501 * XER[S0] is the integer summary overflow bit which is nothing
502 * to do NX. Since this bit can be set with other return values,
508 case ICSWX_INITIATED
:
509 ret
= wait_for_csb(wmem
, csb
);
512 pr_debug_ratelimited("842 Coprocessor busy\n");
516 pr_err_ratelimited("ICSWX rejected\n");
522 *outlenp
= be32_to_cpu(csb
->count
);
528 * nx842_exec_vas - compress/decompress data using the 842 algorithm
530 * (De)compression provided by the NX842 coprocessor on IBM PowerNV systems.
531 * This compresses or decompresses the provided input buffer into the provided
534 * Upon return from this function @outlen contains the length of the
535 * output data. If there is an error then @outlen will be 0 and an
536 * error will be specified by the return code from this function.
538 * The @workmem buffer should only be used by one function call at a time.
540 * @in: input buffer pointer
541 * @inlen: input buffer size
542 * @out: output buffer pointer
543 * @outlenp: output buffer size pointer
544 * @workmem: working memory buffer pointer, size determined by
545 * nx842_powernv_driver.workmem_size
546 * @fc: function code, see CCW Function Codes in nx-842.h
549 * 0 Success, output of length @outlenp stored in the buffer
551 * -ENODEV Hardware unavailable
552 * -ENOSPC Output buffer is to small
553 * -EMSGSIZE Input buffer too large
554 * -EINVAL buffer constraints do not fix nx842_constraints
555 * -EPROTO hardware error during operation
556 * -ETIMEDOUT hardware did not complete operation in reasonable time
557 * -EINTR operation was aborted
559 static int nx842_exec_vas(const unsigned char *in
, unsigned int inlen
,
560 unsigned char *out
, unsigned int *outlenp
,
561 void *workmem
, int fc
)
563 struct coprocessor_request_block
*crb
;
564 struct coprocessor_status_block
*csb
;
565 struct nx842_workmem
*wmem
;
566 struct vas_window
*txwin
;
569 unsigned int outlen
= *outlenp
;
571 wmem
= PTR_ALIGN(workmem
, WORKMEM_ALIGN
);
578 ret
= nx842_config_crb(in
, inlen
, out
, outlen
, wmem
);
583 ccw
= SET_FIELD(CCW_FC_842
, ccw
, fc
);
584 crb
->ccw
= cpu_to_be32(ccw
);
587 wmem
->start
= ktime_get();
589 txwin
= this_cpu_read(cpu_txwin
);
592 * VAS copy CRB into L2 cache. Refer <asm/vas.h>.
595 vas_copy_crb(crb
, 0);
598 * VAS paste previously copied CRB to NX.
599 * @txwin, @offset and @last (must be true).
601 ret
= vas_paste_crb(txwin
, 0, 1);
604 * Retry copy/paste function for VAS failures.
606 } while (ret
&& (i
++ < VAS_RETRIES
));
609 pr_err_ratelimited("VAS copy/paste failed\n");
613 ret
= wait_for_csb(wmem
, csb
);
615 *outlenp
= be32_to_cpu(csb
->count
);
621 * nx842_powernv_compress - Compress data using the 842 algorithm
623 * Compression provided by the NX842 coprocessor on IBM PowerNV systems.
624 * The input buffer is compressed and the result is stored in the
625 * provided output buffer.
627 * Upon return from this function @outlen contains the length of the
628 * compressed data. If there is an error then @outlen will be 0 and an
629 * error will be specified by the return code from this function.
631 * @in: input buffer pointer
632 * @inlen: input buffer size
633 * @out: output buffer pointer
634 * @outlenp: output buffer size pointer
635 * @workmem: working memory buffer pointer, size determined by
636 * nx842_powernv_driver.workmem_size
638 * Returns: see @nx842_powernv_exec()
640 static int nx842_powernv_compress(const unsigned char *in
, unsigned int inlen
,
641 unsigned char *out
, unsigned int *outlenp
,
644 return nx842_powernv_exec(in
, inlen
, out
, outlenp
,
645 wmem
, CCW_FC_842_COMP_CRC
);
649 * nx842_powernv_decompress - Decompress data using the 842 algorithm
651 * Decompression provided by the NX842 coprocessor on IBM PowerNV systems.
652 * The input buffer is decompressed and the result is stored in the
653 * provided output buffer.
655 * Upon return from this function @outlen contains the length of the
656 * decompressed data. If there is an error then @outlen will be 0 and an
657 * error will be specified by the return code from this function.
659 * @in: input buffer pointer
660 * @inlen: input buffer size
661 * @out: output buffer pointer
662 * @outlenp: output buffer size pointer
663 * @workmem: working memory buffer pointer, size determined by
664 * nx842_powernv_driver.workmem_size
666 * Returns: see @nx842_powernv_exec()
668 static int nx842_powernv_decompress(const unsigned char *in
, unsigned int inlen
,
669 unsigned char *out
, unsigned int *outlenp
,
672 return nx842_powernv_exec(in
, inlen
, out
, outlenp
,
673 wmem
, CCW_FC_842_DECOMP_CRC
);
676 static inline void nx_add_coprocs_list(struct nx_coproc
*coproc
,
679 coproc
->chip_id
= chipid
;
680 INIT_LIST_HEAD(&coproc
->list
);
681 list_add(&coproc
->list
, &nx_coprocs
);
684 static struct vas_window
*nx_alloc_txwin(struct nx_coproc
*coproc
)
686 struct vas_window
*txwin
= NULL
;
687 struct vas_tx_win_attr txattr
;
690 * Kernel requests will be high priority. So open send
691 * windows only for high priority RxFIFO entries.
693 vas_init_tx_win_attr(&txattr
, coproc
->ct
);
694 txattr
.lpid
= 0; /* lpid is 0 for kernel requests */
697 * Open a VAS send window which is used to send request to NX.
699 txwin
= vas_tx_win_open(coproc
->vas
.id
, coproc
->ct
, &txattr
);
701 pr_err("ibm,nx-842: Can not open TX window: %ld\n",
708 * Identify chip ID for each CPU, open send wndow for the corresponding NX
709 * engine and save txwin in percpu cpu_txwin.
710 * cpu_txwin is used in copy/paste operation for each compression /
711 * decompression request.
713 static int nx_open_percpu_txwins(void)
715 struct nx_coproc
*coproc
, *n
;
716 unsigned int i
, chip_id
;
718 for_each_possible_cpu(i
) {
719 struct vas_window
*txwin
= NULL
;
721 chip_id
= cpu_to_chip_id(i
);
723 list_for_each_entry_safe(coproc
, n
, &nx_coprocs
, list
) {
725 * Kernel requests use only high priority FIFOs. So
726 * open send windows for these FIFOs.
727 * GZIP is not supported in kernel right now.
730 if (coproc
->ct
!= VAS_COP_TYPE_842_HIPRI
)
733 if (coproc
->chip_id
== chip_id
) {
734 txwin
= nx_alloc_txwin(coproc
);
736 return PTR_ERR(txwin
);
738 per_cpu(cpu_txwin
, i
) = txwin
;
743 if (!per_cpu(cpu_txwin
, i
)) {
744 /* shouldn't happen, Each chip will have NX engine */
745 pr_err("NX engine is not available for CPU %d\n", i
);
753 static int __init
nx_set_ct(struct nx_coproc
*coproc
, const char *priority
,
754 int high
, int normal
)
756 if (!strcmp(priority
, "High"))
758 else if (!strcmp(priority
, "Normal"))
761 pr_err("Invalid RxFIFO priority value\n");
768 static int __init
vas_cfg_coproc_info(struct device_node
*dn
, int chip_id
,
769 int vasid
, int type
, int *ct
)
771 struct vas_window
*rxwin
= NULL
;
772 struct vas_rx_win_attr rxattr
;
773 u32 lpid
, pid
, tid
, fifo_size
;
774 struct nx_coproc
*coproc
;
776 const char *priority
;
779 ret
= of_property_read_u64(dn
, "rx-fifo-address", &rx_fifo
);
781 pr_err("Missing rx-fifo-address property\n");
785 ret
= of_property_read_u32(dn
, "rx-fifo-size", &fifo_size
);
787 pr_err("Missing rx-fifo-size property\n");
791 ret
= of_property_read_u32(dn
, "lpid", &lpid
);
793 pr_err("Missing lpid property\n");
797 ret
= of_property_read_u32(dn
, "pid", &pid
);
799 pr_err("Missing pid property\n");
803 ret
= of_property_read_u32(dn
, "tid", &tid
);
805 pr_err("Missing tid property\n");
809 ret
= of_property_read_string(dn
, "priority", &priority
);
811 pr_err("Missing priority property\n");
815 coproc
= kzalloc(sizeof(*coproc
), GFP_KERNEL
);
819 if (type
== NX_CT_842
)
820 ret
= nx_set_ct(coproc
, priority
, VAS_COP_TYPE_842_HIPRI
,
822 else if (type
== NX_CT_GZIP
)
823 ret
= nx_set_ct(coproc
, priority
, VAS_COP_TYPE_GZIP_HIPRI
,
829 vas_init_rx_win_attr(&rxattr
, coproc
->ct
);
830 rxattr
.rx_fifo
= (void *)rx_fifo
;
831 rxattr
.rx_fifo_size
= fifo_size
;
832 rxattr
.lnotify_lpid
= lpid
;
833 rxattr
.lnotify_pid
= pid
;
834 rxattr
.lnotify_tid
= tid
;
836 * Maximum RX window credits can not be more than #CRBs in
837 * RxFIFO. Otherwise, can get checkstop if RxFIFO overruns.
839 rxattr
.wcreds_max
= fifo_size
/ CRB_SIZE
;
842 * Open a VAS receice window which is used to configure RxFIFO
845 rxwin
= vas_rx_win_open(vasid
, coproc
->ct
, &rxattr
);
847 ret
= PTR_ERR(rxwin
);
848 pr_err("setting RxFIFO with VAS failed: %d\n",
853 coproc
->vas
.rxwin
= rxwin
;
854 coproc
->vas
.id
= vasid
;
855 nx_add_coprocs_list(coproc
, chip_id
);
858 * (lpid, pid, tid) combination has to be unique for each
859 * coprocessor instance in the system. So to make it
860 * unique, skiboot uses coprocessor type such as 842 or
861 * GZIP for pid and provides this value to kernel in pid
862 * device-tree property.
873 static int __init
nx_coproc_init(int chip_id
, int ct_842
, int ct_gzip
)
877 if (opal_check_token(OPAL_NX_COPROC_INIT
)) {
878 ret
= opal_nx_coproc_init(chip_id
, ct_842
);
881 ret
= opal_nx_coproc_init(chip_id
, ct_gzip
);
884 ret
= opal_error_code(ret
);
885 pr_err("Failed to initialize NX for chip(%d): %d\n",
889 pr_warn("Firmware doesn't support NX initialization\n");
894 static int __init
find_nx_device_tree(struct device_node
*dn
, int chip_id
,
895 int vasid
, int type
, char *devname
,
900 if (of_device_is_compatible(dn
, devname
)) {
901 ret
= vas_cfg_coproc_info(dn
, chip_id
, vasid
, type
, ct
);
909 static int __init
nx_powernv_probe_vas(struct device_node
*pn
)
911 int chip_id
, vasid
, ret
= 0;
912 int ct_842
= 0, ct_gzip
= 0;
913 struct device_node
*dn
;
915 chip_id
= of_get_ibm_chip_id(pn
);
917 pr_err("ibm,chip-id missing\n");
921 vasid
= chip_to_vas_id(chip_id
);
923 pr_err("Unable to map chip_id %d to vasid\n", chip_id
);
927 for_each_child_of_node(pn
, dn
) {
928 ret
= find_nx_device_tree(dn
, chip_id
, vasid
, NX_CT_842
,
929 "ibm,p9-nx-842", &ct_842
);
932 ret
= find_nx_device_tree(dn
, chip_id
, vasid
,
933 NX_CT_GZIP
, "ibm,p9-nx-gzip", &ct_gzip
);
939 if (!ct_842
|| !ct_gzip
) {
940 pr_err("NX FIFO nodes are missing\n");
945 * Initialize NX instance for both high and normal priority FIFOs.
947 ret
= nx_coproc_init(chip_id
, ct_842
, ct_gzip
);
952 static int __init
nx842_powernv_probe(struct device_node
*dn
)
954 struct nx_coproc
*coproc
;
958 chip_id
= of_get_ibm_chip_id(dn
);
960 pr_err("ibm,chip-id missing\n");
964 if (of_property_read_u32(dn
, "ibm,842-coprocessor-type", &ct
)) {
965 pr_err("ibm,842-coprocessor-type missing\n");
969 if (of_property_read_u32(dn
, "ibm,842-coprocessor-instance", &ci
)) {
970 pr_err("ibm,842-coprocessor-instance missing\n");
974 coproc
= kzalloc(sizeof(*coproc
), GFP_KERNEL
);
980 nx_add_coprocs_list(coproc
, chip_id
);
982 pr_info("coprocessor found on chip %d, CT %d CI %d\n", chip_id
, ct
, ci
);
986 else if (nx842_ct
!= ct
)
987 pr_err("NX842 chip %d, CT %d != first found CT %d\n",
988 chip_id
, ct
, nx842_ct
);
993 static void nx_delete_coprocs(void)
995 struct nx_coproc
*coproc
, *n
;
996 struct vas_window
*txwin
;
1000 * close percpu txwins that are opened for the corresponding coproc.
1002 for_each_possible_cpu(i
) {
1003 txwin
= per_cpu(cpu_txwin
, i
);
1005 vas_win_close(txwin
);
1007 per_cpu(cpu_txwin
, i
) = NULL
;
1010 list_for_each_entry_safe(coproc
, n
, &nx_coprocs
, list
) {
1011 if (coproc
->vas
.rxwin
)
1012 vas_win_close(coproc
->vas
.rxwin
);
1014 list_del(&coproc
->list
);
1019 static struct nx842_constraints nx842_powernv_constraints
= {
1020 .alignment
= DDE_BUFFER_ALIGN
,
1021 .multiple
= DDE_BUFFER_LAST_MULT
,
1022 .minimum
= DDE_BUFFER_LAST_MULT
,
1023 .maximum
= (DDL_LEN_MAX
- 1) * PAGE_SIZE
,
1026 static struct nx842_driver nx842_powernv_driver
= {
1027 .name
= KBUILD_MODNAME
,
1028 .owner
= THIS_MODULE
,
1029 .workmem_size
= sizeof(struct nx842_workmem
),
1030 .constraints
= &nx842_powernv_constraints
,
1031 .compress
= nx842_powernv_compress
,
1032 .decompress
= nx842_powernv_decompress
,
1035 static int nx842_powernv_crypto_init(struct crypto_tfm
*tfm
)
1037 return nx842_crypto_init(tfm
, &nx842_powernv_driver
);
1040 static struct crypto_alg nx842_powernv_alg
= {
1042 .cra_driver_name
= "842-nx",
1043 .cra_priority
= 300,
1044 .cra_flags
= CRYPTO_ALG_TYPE_COMPRESS
,
1045 .cra_ctxsize
= sizeof(struct nx842_crypto_ctx
),
1046 .cra_module
= THIS_MODULE
,
1047 .cra_init
= nx842_powernv_crypto_init
,
1048 .cra_exit
= nx842_crypto_exit
,
1049 .cra_u
= { .compress
= {
1050 .coa_compress
= nx842_crypto_compress
,
1051 .coa_decompress
= nx842_crypto_decompress
} }
1054 static __init
int nx_compress_powernv_init(void)
1056 struct device_node
*dn
;
1059 /* verify workmem size/align restrictions */
1060 BUILD_BUG_ON(WORKMEM_ALIGN
% CRB_ALIGN
);
1061 BUILD_BUG_ON(CRB_ALIGN
% DDE_ALIGN
);
1062 BUILD_BUG_ON(CRB_SIZE
% DDE_ALIGN
);
1063 /* verify buffer size/align restrictions */
1064 BUILD_BUG_ON(PAGE_SIZE
% DDE_BUFFER_ALIGN
);
1065 BUILD_BUG_ON(DDE_BUFFER_ALIGN
% DDE_BUFFER_SIZE_MULT
);
1066 BUILD_BUG_ON(DDE_BUFFER_SIZE_MULT
% DDE_BUFFER_LAST_MULT
);
1068 for_each_compatible_node(dn
, NULL
, "ibm,power9-nx") {
1069 ret
= nx_powernv_probe_vas(dn
);
1071 nx_delete_coprocs();
1077 if (list_empty(&nx_coprocs
)) {
1078 for_each_compatible_node(dn
, NULL
, "ibm,power-nx")
1079 nx842_powernv_probe(dn
);
1084 nx842_powernv_exec
= nx842_exec_icswx
;
1087 * Register VAS user space API for NX GZIP so
1088 * that user space can use GZIP engine.
1089 * Using high FIFO priority for kernel requests and
1090 * normal FIFO priority is assigned for userspace.
1091 * 842 compression is supported only in kernel.
1093 ret
= vas_register_coproc_api(THIS_MODULE
, VAS_COP_TYPE_GZIP
,
1097 * GZIP is not supported in kernel right now.
1098 * So open tx windows only for 842.
1101 ret
= nx_open_percpu_txwins();
1104 nx_delete_coprocs();
1108 nx842_powernv_exec
= nx842_exec_vas
;
1111 ret
= crypto_register_alg(&nx842_powernv_alg
);
1113 nx_delete_coprocs();
1119 module_init(nx_compress_powernv_init
);
1121 static void __exit
nx_compress_powernv_exit(void)
1124 * GZIP engine is supported only in power9 or later and nx842_ct
1125 * is used on power8 (icswx).
1126 * VAS API for NX GZIP is registered during init for user space
1127 * use. So delete this API use for GZIP engine.
1130 vas_unregister_coproc_api();
1132 crypto_unregister_alg(&nx842_powernv_alg
);
1134 nx_delete_coprocs();
1136 module_exit(nx_compress_powernv_exit
);