Home | History | Annotate | Line # | Download | only in acr
      1 /*	$NetBSD: nouveau_nvkm_subdev_acr_hsfw.c,v 1.2 2021/12/18 23:45:38 riastradh Exp $	*/
      2 
      3 /*
      4  * Copyright 2019 Red Hat Inc.
      5  *
      6  * Permission is hereby granted, free of charge, to any person obtaining a
      7  * copy of this software and associated documentation files (the "Software"),
      8  * to deal in the Software without restriction, including without limitation
      9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
     10  * and/or sell copies of the Software, and to permit persons to whom the
     11  * Software is furnished to do so, subject to the following conditions:
     12  *
     13  * The above copyright notice and this permission notice shall be included in
     14  * all copies or substantial portions of the Software.
     15  *
     16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
     19  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
     20  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
     21  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
     22  * OTHER DEALINGS IN THE SOFTWARE.
     23  */
     24 #include <sys/cdefs.h>
     25 __KERNEL_RCSID(0, "$NetBSD: nouveau_nvkm_subdev_acr_hsfw.c,v 1.2 2021/12/18 23:45:38 riastradh Exp $");
     26 
     27 #include "priv.h"
     28 
     29 #include <core/firmware.h>
     30 
     31 #include <nvfw/fw.h>
     32 #include <nvfw/hs.h>
     33 
     34 static void
     35 nvkm_acr_hsfw_del(struct nvkm_acr_hsfw *hsfw)
     36 {
     37 	list_del(&hsfw->head);
     38 	kfree(hsfw->imem);
     39 	kfree(hsfw->image);
     40 	kfree(hsfw->sig.prod.data);
     41 	kfree(hsfw->sig.dbg.data);
     42 	kfree(hsfw);
     43 }
     44 
     45 void
     46 nvkm_acr_hsfw_del_all(struct nvkm_acr *acr)
     47 {
     48 	struct nvkm_acr_hsfw *hsfw, *hsft;
     49 	list_for_each_entry_safe(hsfw, hsft, &acr->hsfw, head) {
     50 		nvkm_acr_hsfw_del(hsfw);
     51 	}
     52 }
     53 
     54 static int
     55 nvkm_acr_hsfw_load_image(struct nvkm_acr *acr, const char *name, int ver,
     56 			 struct nvkm_acr_hsfw *hsfw)
     57 {
     58 	struct nvkm_subdev *subdev = &acr->subdev;
     59 	const struct firmware *fw;
     60 	const struct nvfw_bin_hdr *hdr;
     61 	const struct nvfw_hs_header *fwhdr;
     62 	const struct nvfw_hs_load_header *lhdr;
     63 	u32 loc, sig;
     64 	int ret;
     65 
     66 	ret = nvkm_firmware_get(subdev, name, ver, &fw);
     67 	if (ret < 0)
     68 		return ret;
     69 
     70 	hdr = nvfw_bin_hdr(subdev, fw->data);
     71 	fwhdr = nvfw_hs_header(subdev, fw->data + hdr->header_offset);
     72 
     73 	/* Earlier FW releases by NVIDIA for Nouveau's use aren't in NVIDIA's
     74 	 * standard format, and don't have the indirection seen in the 0x10de
     75 	 * case.
     76 	 */
     77 	switch (hdr->bin_magic) {
     78 	case 0x000010de:
     79 		loc = *(u32 *)(fw->data + fwhdr->patch_loc);
     80 		sig = *(u32 *)(fw->data + fwhdr->patch_sig);
     81 		break;
     82 	case 0x3b1d14f0:
     83 		loc = fwhdr->patch_loc;
     84 		sig = fwhdr->patch_sig;
     85 		break;
     86 	default:
     87 		ret = -EINVAL;
     88 		goto done;
     89 	}
     90 
     91 	lhdr = nvfw_hs_load_header(subdev, fw->data + fwhdr->hdr_offset);
     92 
     93 	if (!(hsfw->image = kmalloc(hdr->data_size, GFP_KERNEL))) {
     94 		ret = -ENOMEM;
     95 		goto done;
     96 	}
     97 
     98 	memcpy(hsfw->image, fw->data + hdr->data_offset, hdr->data_size);
     99 	hsfw->image_size = hdr->data_size;
    100 	hsfw->non_sec_addr = lhdr->non_sec_code_off;
    101 	hsfw->non_sec_size = lhdr->non_sec_code_size;
    102 	hsfw->sec_addr = lhdr->apps[0];
    103 	hsfw->sec_size = lhdr->apps[lhdr->num_apps];
    104 	hsfw->data_addr = lhdr->data_dma_base;
    105 	hsfw->data_size = lhdr->data_size;
    106 
    107 	hsfw->sig.prod.size = fwhdr->sig_prod_size;
    108 	hsfw->sig.prod.data = kmalloc(hsfw->sig.prod.size, GFP_KERNEL);
    109 	if (!hsfw->sig.prod.data) {
    110 		ret = -ENOMEM;
    111 		goto done;
    112 	}
    113 
    114 	memcpy(hsfw->sig.prod.data, fw->data + fwhdr->sig_prod_offset + sig,
    115 	       hsfw->sig.prod.size);
    116 
    117 	hsfw->sig.dbg.size = fwhdr->sig_dbg_size;
    118 	hsfw->sig.dbg.data = kmalloc(hsfw->sig.dbg.size, GFP_KERNEL);
    119 	if (!hsfw->sig.dbg.data) {
    120 		ret = -ENOMEM;
    121 		goto done;
    122 	}
    123 
    124 	memcpy(hsfw->sig.dbg.data, fw->data + fwhdr->sig_dbg_offset + sig,
    125 	       hsfw->sig.dbg.size);
    126 
    127 	hsfw->sig.patch_loc = loc;
    128 done:
    129 	nvkm_firmware_put(fw);
    130 	return ret;
    131 }
    132 
    133 static int
    134 nvkm_acr_hsfw_load_bl(struct nvkm_acr *acr, const char *name, int ver,
    135 		      struct nvkm_acr_hsfw *hsfw)
    136 {
    137 	struct nvkm_subdev *subdev = &acr->subdev;
    138 	const struct nvfw_bin_hdr *hdr;
    139 	const struct nvfw_bl_desc *desc;
    140 	const struct firmware *fw;
    141 	u8 *data;
    142 	int ret;
    143 
    144 	ret = nvkm_firmware_get(subdev, name, ver, &fw);
    145 	if (ret)
    146 		return ret;
    147 
    148 	hdr = nvfw_bin_hdr(subdev, fw->data);
    149 	desc = nvfw_bl_desc(subdev, fw->data + hdr->header_offset);
    150 	data = (void *)fw->data + hdr->data_offset;
    151 
    152 	hsfw->imem_size = desc->code_size;
    153 	hsfw->imem_tag = desc->start_tag;
    154 	hsfw->imem = kmalloc(desc->code_size, GFP_KERNEL);
    155 	memcpy(hsfw->imem, data + desc->code_off, desc->code_size);
    156 
    157 	nvkm_firmware_put(fw);
    158 	return 0;
    159 }
    160 
    161 int
    162 nvkm_acr_hsfw_load(struct nvkm_acr *acr, const char *bl, const char *fw,
    163 		   const char *name, int version,
    164 		   const struct nvkm_acr_hsf_fwif *fwif)
    165 {
    166 	struct nvkm_acr_hsfw *hsfw;
    167 	int ret;
    168 
    169 	if (!(hsfw = kzalloc(sizeof(*hsfw), GFP_KERNEL)))
    170 		return -ENOMEM;
    171 
    172 	hsfw->func = fwif->func;
    173 	hsfw->name = name;
    174 	list_add_tail(&hsfw->head, &acr->hsfw);
    175 
    176 	ret = nvkm_acr_hsfw_load_bl(acr, bl, version, hsfw);
    177 	if (ret)
    178 		goto done;
    179 
    180 	ret = nvkm_acr_hsfw_load_image(acr, fw, version, hsfw);
    181 done:
    182 	if (ret)
    183 		nvkm_acr_hsfw_del(hsfw);
    184 	return ret;
    185 }
    186