qe: add ability to upload QE firmware
Define the layout of a binary blob that contains a QE firmware and instructions on how to upload it. Add function qe_upload_microcode() to parse the blob and perform the actual upload. Fully define 'struct rsp' in immap_qe.h to include the actual RISC Special Registers.
---
This patch applies on top of my previous patches
"qe: add function qe_clock_source" and
"ucc_geth: use rx-clock-name and tx-clock-name device tree properties".
This patch defines a new specification for a QE binary file. Please review the specification as well as the code.
arch/powerpc/sysdev/qe_lib/qe.c | 96 +++++++++++++++++++++++++++++ include/asm-powerpc/immap_qe.h | 29 ++++++++- include/asm-powerpc/qe.h | 128 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 251 insertions(+), 2 deletions(-)
arch/powerpc/sysdev/qe_lib/qe.c +96 -0
@@ -25,6 +25,7 @@#include <linux/module.h> #include <linux/delay.h> #include <linux/ioport.h> +#include <linux/crc32.h> #include <asm/irq.h> #include <asm/page.h> #include <asm/pgtable.h>
@@ -393,3 +394,98 @@ void *qe_muram_addr(unsigned long offset)return (void *)&qe_immr->muram[offset]; }EXPORT_SYMBOL (qe_muram_addr); + +/* +* Download a microcode to the I-RAM at a specific address. +* +* @firmware: pointer to qe_firmware structure +*/ +int qe_upload_microcode (const struct qe_firmware *firmware) +{ +unsigned int i; +unsigned int j; +__be32 *code; +u32 crc; +size_t length =sizeof (struct qe_firmware ); + +/* Check the magic and version number */ +if ((firmware->magic[0 ] !='Q' ) || (firmware->magic[1 ] !='E' ) || + (firmware->magic[2 ] !='F' ) || (firmware->version !=1 )) { +printk (KERN_ERR "QE: invalid or unsupported microcode\n" ); +return -EPERM ; + } + +/* Validate the length and check if there's a CRC */ +for (i =0 ; i < firmware->count; i++) + length += firmware->microcode[i].count *4 ; + +if (firmware->length == (length +sizeof (u32))) { +/* Length is valid, and there's a CRC */ + crc =be32_to_cpu (*((__be32 *) ((void *) firmware + length))); +if (crc !=crc32 (0 , firmware, length)) { +printk (KERN_ERR "QE: firmware CRC is invalid\n" ); +return -EIO ; + } + }else if (firmware->length != length) { +printk (KERN_ERR "QE: invalid length(s) in firware structure\n" ); +return -EPERM ; + } + +/* If there's only one microcode, then we assume it's common for all +RISCs, so we set the CERCR.CIR bit to share the IRAM with all RISCs. +This should be safe even on SOCs with only one RISC. + +If there are multiple 'microcode' structures, but each one points +to the same microcode binary (ignoring offsets), then we also assume +that we want share RAM. +*/ +if (firmware->count ==1 ) +setbits16 (&qe_immr->cp.cercr,cpu_to_be16 (0x800 )); +else { +for (i =1 ; i < firmware->count; i++) +if (firmware->microcode[i].code_offset != + firmware->microcode[0 ].code_offset) +break ; + +if (i == firmware->count) +setbits16 (&qe_immr->cp.cercr,cpu_to_be16 (0x800 )); + } + +if (firmware->soc.model) +printk (KERN_INFO "QE: uploading microcode '%s' for %u V%u.%u\n" , + firmware->id,be16_to_cpu (firmware->soc.model), + firmware->soc.rev_h, firmware->soc.rev_l); +else +printk (KERN_INFO "QE: uploading microcode '%s'\n" , + firmware->id); + +for (i =0 ; i < firmware->count; i++) { +const struct qe_microcode *ucode = &firmware->microcode[i]; + + code = (void *) firmware +be32_to_cpu (ucode->code_offset); + +/* Use auto-increment */ +out_be32 (&qe_immr->iram.iadd,cpu_to_be32 (0x80080000 | +be32_to_cpu (ucode->iram_offset))); + +for (j =0 ; j < ucode->count; j++) +out_be32 (&qe_immr->iram.idata,be32_to_cpu (code[j])); + +/* Program the traps. We program both RISCs, even on platforms +that only have one. This *should* be safe. */ +for (j =0 ; j <16 ; j++) +if (ucode->traps[j]) { +u32 trap =be32_to_cpu (ucode->traps[j]); +out_be32 (&qe_immr->rsp[0 ].tibcr[j], trap); +out_be32 (&qe_immr->rsp[1 ].tibcr[j], trap); + } + } + +/* Enable the traps for both RISCs. Again, on a single-RISC system, +this should be safe. */ +out_be32 (&qe_immr->rsp[0 ].eccr,cpu_to_be32 (0x20800000 )); +out_be32 (&qe_immr->rsp[1 ].eccr,cpu_to_be32 (0x20800000 )); + +return 0 ; +} +EXPORT_SYMBOL (qe_upload_microcode);
include/asm-powerpc/immap_qe.h +27 -2
@@ -395,8 +395,33 @@ struct dbg {/* RISC Special Registers (Trap and Breakpoint) */ struct rsp { -u32 reg[0x40 ];/* 64 32-bit registers */ -} __attribute__ ((packed)); +__be32 tibcr[16 ]; +u8 res0[64 ]; +__be32 ibcr0; +__be32 ibs0; +__be32 ibcnr0; +u8 res1[4 ]; +__be32 ibcr1; +__be32 ibs1; +__be32 ibcnr1; +__be32 npcr; +__be32 dbcr; +__be32 dbar; +__be32 dbamr; +__be32 dbsr; +__be32 dbcnr; +u8 res2[12 ]; +__be32 dbdr_h; +__be32 dbdr_l; +__be32 dbdmr_h; +__be32 dbdmr_l; +__be32 bsr; +__be32 bor; +u8 res3[24 ]; +__be32 eccr; +__be32 eicr; +u8 res4[0x100 -0xf8 ]; +};struct qe_immap {struct qe_iram iram;/* I-RAM */
include/asm-powerpc/qe.h +128 -0
134 lines not on this page · raw message
Signature · 1 line
1.5.2.4
Quoted 6 lines · Timur Tabi
Define the layout of a binary blob that contains a QE firmware and instructions on how to upload it. Add function
qe_upload_microcode()to parse the blob and perform the actual upload. Fully define 'struct rsp' inimmap_qe.hto include the actual RISC Special Registers.Signed-off-by Timur Tabi <[email protected]>
I need to add a few fields to the structure, but I would still appreciate comments on the structure and the corresponding code.
Signature · 2 lines
Timur Tabi Linux Kernel Developer @ Freescale
Quoted 4 lines · Timur Tabi
Define the layout of a binary blob that contains a QE firmware and instructions on how to upload it. Add function
qe_upload_microcode()to parse the blob and perform the actual upload. Fully define 'struct rsp' inimmap_qe.hto include the actual RISC Special Registers.
[...]
Quoted 2 lines
I need to add a few fields to the structure, but I would still appreciate comments on the structure and the corresponding code.
It seems I'm too lazy to go deep inside the code, but it's okay... because I believe it is works fine. ;-)
Thus sorry, only fluffy cosmetic notes.
Quoted 2 lines
Signed-off-by Timur Tabi <[email protected]>---
[...]
arch/powerpc/sysdev/qe_lib/qe.c void *qe_muram_addr(unsigned long offset) · line 424
Quoted 3 lines
arch/powerpc/sysdev/qe_lib/qe.c +3
-0+if (firmware->length == (length +sizeof (u32))) { +/* Length is valid, and there's a CRC */ + crc =be32_to_cpu (*((__be32 *) ((void *) firmware + length)));
Spaces are not needed after "(__be32 *)" and "(void *)". The whole construction isn't easy to parse, thus spaces are only distracting.
Quoted 7 lines
arch/powerpc/sysdev/qe_lib/qe.c +7
-0+if (crc !=crc32 (0 , firmware, length)) { +printk (KERN_ERR "QE: firmware CRC is invalid\n" ); +return -EIO ; + } + }else if (firmware->length != length) { +printk (KERN_ERR "QE: invalid length(s) in firware structure\n" ); +return -EPERM ;
arch/powerpc/sysdev/qe_lib/qe.c void *qe_muram_addr(unsigned long offset) · line 441
Quoted 10 lines
arch/powerpc/sysdev/qe_lib/qe.c +10
-0+ } + +/* If there's only one microcode, then we assume it's common for all +RISCs, so we set the CERCR.CIR bit to share the IRAM with all RISCs. +This should be safe even on SOCs with only one RISC. + +If there are multiple 'microcode' structures, but each one points +to the same microcode binary (ignoring offsets), then we also assume +that we want share RAM. +*/
Comment style is unorthodox.
arch/powerpc/sysdev/qe_lib/qe.c void *qe_muram_addr(unsigned long offset) · line 444
Quoted 3 lines
arch/powerpc/sysdev/qe_lib/qe.c +3
-0+if (firmware->count ==1 ) +setbits16 (&qe_immr->cp.cercr,cpu_to_be16 (0x800 )); +else {
I'd place braces for the first 'if'. No lines added, more good looking. The CodingStyle also suggest this.
arch/powerpc/sysdev/qe_lib/qe.c void *qe_muram_addr(unsigned long offset) · line 445
Quoted 1 line
arch/powerpc/sysdev/qe_lib/qe.c +1
-0+for (i =1 ; i < firmware->count; i++)
I'd place braces here too. No rationale though, just better looking.
arch/powerpc/sysdev/qe_lib/qe.c void *qe_muram_addr(unsigned long offset) · line 451
Quoted 6 lines
arch/powerpc/sysdev/qe_lib/qe.c +6
-0+if (firmware->microcode[i].code_offset != + firmware->microcode[0 ].code_offset) +break ; + +if (i == firmware->count) +setbits16 (&qe_immr->cp.cercr,cpu_to_be16 (0x800 ));
0x800? Desires proper #define.
Quoted 4 lines
arch/powerpc/sysdev/qe_lib/qe.c +4
-0+ } + +if (firmware->soc.model) + printk(KERN_INFO "QE: uploading microcode '%s' for %u V%u.%u\n",
arch/powerpc/sysdev/qe_lib/qe.c void *qe_muram_addr(unsigned long offset) · line 465
Quoted 10 lines
arch/powerpc/sysdev/qe_lib/qe.c +10
-0+ firmware->id,be16_to_cpu (firmware->soc.model), + firmware->soc.rev_h, firmware->soc.rev_l); + else + printk(KERN_INFO "QE: uploading microcode '%s'\n", + firmware->id); + +for (i =0 ; i < firmware->count; i++) { +const struct qe_microcode *ucode = &firmware->microcode[i]; + + code = (void *) firmware +be32_to_cpu (ucode->code_offset);
space after (void *).
arch/powerpc/sysdev/qe_lib/qe.c void *qe_muram_addr(unsigned long offset) · line 468
Quoted 2 lines
arch/powerpc/sysdev/qe_lib/qe.c +2
-0+/* Use auto-increment */ + out_be32(&qe_immr->iram.iadd, cpu_to_be32(0x80080000 |
magic number.
arch/powerpc/sysdev/qe_lib/qe.c void *qe_muram_addr(unsigned long offset) · line 479
Quoted 11 lines
arch/powerpc/sysdev/qe_lib/qe.c +11
-0+ be32_to_cpu(ucode->iram_offset))); + +for (j =0 ; j < ucode->count; j++) +out_be32 (&qe_immr->iram.idata,be32_to_cpu (code[j])); + +/* Program the traps. We program both RISCs, even on platforms +that only have one. This *should* be safe. */ +for (j =0 ; j <16 ; j++) +if (ucode->traps[j]) { +u32 trap =be32_to_cpu (ucode->traps[j]); +out_be32 (&qe_immr->rsp[0 ].tibcr[j], trap);
Empty line needed after variable declaration.
arch/powerpc/sysdev/qe_lib/qe.c void *qe_muram_addr(unsigned long offset) · line 485
Quoted 6 lines
arch/powerpc/sysdev/qe_lib/qe.c +6
-0+out_be32 (&qe_immr->rsp[1 ].tibcr[j], trap); + } + } + +/* Enable the traps for both RISCs. Again, on a single-RISC system, +this should be safe. */
Comment.
arch/powerpc/sysdev/qe_lib/qe.c void *qe_muram_addr(unsigned long offset) · line 487
Quoted 2 lines
arch/powerpc/sysdev/qe_lib/qe.c +2
-0+out_be32 (&qe_immr->rsp[0 ].eccr,cpu_to_be32 (0x20800000 )); +out_be32 (&qe_immr->rsp[1 ].eccr,cpu_to_be32 (0x20800000 ));
magic numbers.
Thanks,
Signature · 4 lines
Anton Vorontsov email: [email protected] backup email: [email protected] irc://irc.freenode.net/bd2
Quoted 6 lines · Anton Vorontsov
arch/powerpc/sysdev/qe_lib/qe.c void *qe_muram_addr(unsigned long offset) · line 424
Quoted 3 lines
arch/powerpc/sysdev/qe_lib/qe.c +3
-0+if (firmware->length == (length +sizeof (u32))) { +/* Length is valid, and there's a CRC */ + crc =be32_to_cpu (*((__be32 *) ((void *) firmware + length)));Spaces are not needed after "(
__be32*)" and "(void *)". The whole construction isn't easy to parse, thus spaces are only distracting.
I have to disagree. I added the spaces to make it easier to read.
Quoted 10 lines
arch/powerpc/sysdev/qe_lib/qe.c void *qe_muram_addr(unsigned long offset) · line 441
Quoted 8 lines
arch/powerpc/sysdev/qe_lib/qe.c +8
-0+/* If there's only one microcode, then we assume it's common for all +RISCs, so we set the CERCR.CIR bit to share the IRAM with all RISCs. +This should be safe even on SOCs with only one RISC. + +If there are multiple 'microcode' structures, but each one points +to the same microcode binary (ignoring offsets), then we also assume +that we want share RAM. +*/ Comment style is unorthodox.
Should I prefix each line with a "*"?
Quoted 3 lines
arch/powerpc/sysdev/qe_lib/qe.c void *qe_muram_addr(unsigned long offset) · line 465
Quoted 1 line
arch/powerpc/sysdev/qe_lib/qe.c +1
-0+ code = (void *) firmware +be32_to_cpu (ucode->code_offset);space after (void *).
Really? This:
code = (void *)firmware + be32_to_cpu(ucode->code_offset);
is harder to read. Without the space, it looks like *)firmware is one word.
I shall apply the other changes. Thanks.
Signature · 2 lines
Timur Tabi Linux Kernel Developer @ Freescale
Quoted 19 lines · Timur Tabi
Quoted 5 lines · Anton Vorontsov
arch/powerpc/sysdev/qe_lib/qe.c void *qe_muram_addr(unsigned long offset) · line 424
Quoted 3 lines
arch/powerpc/sysdev/qe_lib/qe.c +3
-0+if (firmware->length == (length +sizeof (u32))) { +/* Length is valid, and there's a CRC */ + crc =be32_to_cpu (*((__be32 *) ((void *) firmware + length)));Spaces are not needed after "(
__be32*)" and "(void *)". The whole construction isn't easy to parse, thus spaces are only distracting.I have to disagree. I added the spaces to make it easier to read.
Quoted 9 lines
arch/powerpc/sysdev/qe_lib/qe.c void *qe_muram_addr(unsigned long offset) · line 441
Quoted 8 lines
arch/powerpc/sysdev/qe_lib/qe.c +8
-0+/* If there's only one microcode, then we assume it's common for all +RISCs, so we set the CERCR.CIR bit to share the IRAM with all RISCs. +This should be safe even on SOCs with only one RISC. + +If there are multiple 'microcode' structures, but each one points +to the same microcode binary (ignoring offsets), then we also assume +that we want share RAM. +*/ Comment style is unorthodox.
Quoted 1 line
Should I prefix each line with a "*"?
Yup.
Heh... well, ideal multiline comment is:
/*
* Multiline comment here.
*/
But this doesn't matter much, the salt is in "*"s. ;-)
Quoted 9 lines
Quoted 2 lines
arch/powerpc/sysdev/qe_lib/qe.c void *qe_muram_addr(unsigned long offset) · line 465
Quoted 1 line
arch/powerpc/sysdev/qe_lib/qe.c +1
-0+ code = (void *) firmware +be32_to_cpu (ucode->code_offset);space after (void *).
Really? This:
code = (void *)firmware + be32_to_cpu(ucode->code_offset);is harder to read. Without the space, it looks like *)firmware is one word.
:-) I'm not about to argue, this is each own preference.
But to complete my point: I don't care what style exactly is used, what I care about is consistency across the code I'm looking in. 100% consistency is not affordable, of course. But we're all trying, aren't we? (type *)variable is orthodox style, and when I see unusual style, my eyes are itching, and this code is hard to read (to me). So, if everybody will switch to "(type *) variable" style -- I'll just follow.
And again: this is all of minor importance, just my $0.02 for the code consistency.
Thanks,
Signature · 4 lines
Anton Vorontsov email: [email protected] backup email: [email protected] irc://irc.freenode.net/bd2
Quoted 6 lines · Timur Tabi
Define the layout of a binary blob that contains a QE firmware and instructions on how to upload it. Add function
qe_upload_microcode()to parse the blob and perform the actual upload. Fully define 'struct rsp' inimmap_qe.hto include the actual RISC Special Registers.Signed-off-by Timur Tabi <[email protected]>
[snip]
Quoted 2 lines
+/*
+ * Download a microcode to the I-RAM at a specific address.
^^^^^^^^
Quoted 4 lines
+ *
+ * @firmware: pointer toqe_firmwarestructure
+ */
+intqe_upload_microcode(const struct qe_firmware *firmware)
^^^^^^
We seem to be a little conflicted over whether it is an upload or a download. ;-)
gvb
Quoted 1 line · Jerry Van Baren
We seem to be a little conflicted over whether it is an upload or a download. ;-)
Not me.
To the host == download
From the host == upload
Quoted 9 lines · Jerry Van Baren
Quoted 2 lines
+/*
+ * Download a microcode to the I-RAM at a specific address.^^^^^^^^Quoted 4 lines
+ *
+ * @firmware: pointer toqe_firmwarestructure
+ */
+intqe_upload_microcode(const struct qe_firmware *firmware)^^^^^^We seem to be a little conflicted over whether it is an upload or a download. ;-)
Oh, now I see what you're talking about. Duh.