Home | History | Annotate | Line # | Download | only in pci
pci_smccc.c revision 1.1
      1  1.1  jmcneill /* $NetBSD: pci_smccc.c,v 1.1 2021/08/07 21:23:37 jmcneill Exp $ */
      2  1.1  jmcneill 
      3  1.1  jmcneill /*-
      4  1.1  jmcneill  * Copyright (c) 2021 Jared McNeill <jmcneill (at) invisible.ca>
      5  1.1  jmcneill  * All rights reserved.
      6  1.1  jmcneill  *
      7  1.1  jmcneill  * Redistribution and use in source and binary forms, with or without
      8  1.1  jmcneill  * modification, are permitted provided that the following conditions
      9  1.1  jmcneill  * are met:
     10  1.1  jmcneill  * 1. Redistributions of source code must retain the above copyright
     11  1.1  jmcneill  *    notice, this list of conditions and the following disclaimer.
     12  1.1  jmcneill  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1  jmcneill  *    notice, this list of conditions and the following disclaimer in the
     14  1.1  jmcneill  *    documentation and/or other materials provided with the distribution.
     15  1.1  jmcneill  *
     16  1.1  jmcneill  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     17  1.1  jmcneill  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     18  1.1  jmcneill  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     19  1.1  jmcneill  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     20  1.1  jmcneill  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     21  1.1  jmcneill  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     22  1.1  jmcneill  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     23  1.1  jmcneill  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     24  1.1  jmcneill  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25  1.1  jmcneill  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26  1.1  jmcneill  * SUCH DAMAGE.
     27  1.1  jmcneill  */
     28  1.1  jmcneill 
     29  1.1  jmcneill #include <sys/cdefs.h>
     30  1.1  jmcneill __KERNEL_RCSID(0, "$NetBSD: pci_smccc.c,v 1.1 2021/08/07 21:23:37 jmcneill Exp $");
     31  1.1  jmcneill 
     32  1.1  jmcneill #include <sys/param.h>
     33  1.1  jmcneill #include <sys/kernel.h>
     34  1.1  jmcneill 
     35  1.1  jmcneill #include <arm/arm/smccc.h>
     36  1.1  jmcneill #include <arm/pci/pci_smccc.h>
     37  1.1  jmcneill 
     38  1.1  jmcneill /* Minimum SMCCC version required for PCI_VERSION call. */
     39  1.1  jmcneill #define	SMCCC_VERSION_1_1	0x10001
     40  1.1  jmcneill 
     41  1.1  jmcneill /* PCI Configuration Space Access ABI functions */
     42  1.1  jmcneill #define	PCI_VERSION		0x84000130
     43  1.1  jmcneill #define	PCI_FEATURES		0x84000131
     44  1.1  jmcneill #define	PCI_READ		0x84000132
     45  1.1  jmcneill #define	PCI_WRITE		0x84000133
     46  1.1  jmcneill #define	PCI_GET_SEG_INFO	0x84000134
     47  1.1  jmcneill #define	 GET_SEG_INFO_BUS_START		__BITS(7,0)
     48  1.1  jmcneill #define	 GET_SEG_INFO_BUS_END		__BITS(15,8)
     49  1.1  jmcneill 
     50  1.1  jmcneill static int
     51  1.1  jmcneill pci_smccc_call(uint32_t fid,
     52  1.1  jmcneill     register_t arg1, register_t arg2, register_t arg3, register_t arg4,
     53  1.1  jmcneill     register_t *res0, register_t *res1, register_t *res2, register_t *res3)
     54  1.1  jmcneill {
     55  1.1  jmcneill 	static int smccc_ver;
     56  1.1  jmcneill 
     57  1.1  jmcneill 	if (smccc_ver == 0) {
     58  1.1  jmcneill 		smccc_ver = smccc_version();
     59  1.1  jmcneill 	}
     60  1.1  jmcneill 	if (smccc_ver < SMCCC_VERSION_1_1) {
     61  1.1  jmcneill 		return SMCCC_NOT_SUPPORTED;
     62  1.1  jmcneill 	}
     63  1.1  jmcneill 
     64  1.1  jmcneill 	return smccc_call(fid, arg1, arg2, arg3, arg4,
     65  1.1  jmcneill 			  res0, res1, res2, res3);
     66  1.1  jmcneill }
     67  1.1  jmcneill 
     68  1.1  jmcneill int
     69  1.1  jmcneill pci_smccc_version(void)
     70  1.1  jmcneill {
     71  1.1  jmcneill 	return pci_smccc_call(PCI_VERSION, 0, 0, 0, 0,
     72  1.1  jmcneill 			      NULL, NULL, NULL, NULL);
     73  1.1  jmcneill }
     74  1.1  jmcneill 
     75  1.1  jmcneill int
     76  1.1  jmcneill pci_smccc_features(uint32_t fid)
     77  1.1  jmcneill {
     78  1.1  jmcneill 	return pci_smccc_call(PCI_FEATURES, fid, 0, 0, 0,
     79  1.1  jmcneill 			      NULL, NULL, NULL, NULL);
     80  1.1  jmcneill }
     81  1.1  jmcneill 
     82  1.1  jmcneill int
     83  1.1  jmcneill pci_smccc_read(uint32_t sbdf, uint32_t offset, uint32_t access_size,
     84  1.1  jmcneill     uint32_t *data)
     85  1.1  jmcneill {
     86  1.1  jmcneill 	register_t value;
     87  1.1  jmcneill 	int status;
     88  1.1  jmcneill 
     89  1.1  jmcneill 	status = pci_smccc_call(PCI_READ, sbdf, offset, access_size, 0,
     90  1.1  jmcneill 				NULL, &value, NULL, NULL);
     91  1.1  jmcneill 	if (status == SMCCC_SUCCESS) {
     92  1.1  jmcneill 		*data = value;
     93  1.1  jmcneill 	}
     94  1.1  jmcneill 
     95  1.1  jmcneill 	return status;
     96  1.1  jmcneill }
     97  1.1  jmcneill 
     98  1.1  jmcneill int
     99  1.1  jmcneill pci_smccc_write(uint32_t sbdf, uint32_t offset, uint32_t access_size,
    100  1.1  jmcneill     uint32_t data)
    101  1.1  jmcneill {
    102  1.1  jmcneill 	return pci_smccc_call(PCI_WRITE, sbdf, offset, access_size, data,
    103  1.1  jmcneill 			      NULL, NULL, NULL, NULL);
    104  1.1  jmcneill }
    105  1.1  jmcneill 
    106  1.1  jmcneill int
    107  1.1  jmcneill pci_smccc_get_seg_info(uint16_t seg, uint8_t *bus_start, uint8_t *bus_end,
    108  1.1  jmcneill     uint16_t *next_seg)
    109  1.1  jmcneill {
    110  1.1  jmcneill 	register_t res1, res2;
    111  1.1  jmcneill 	int status;
    112  1.1  jmcneill 
    113  1.1  jmcneill 	status = pci_smccc_call(PCI_GET_SEG_INFO, seg, 0, 0, 0,
    114  1.1  jmcneill 				NULL, &res1, &res2, NULL);
    115  1.1  jmcneill 	if (status == SMCCC_SUCCESS) {
    116  1.1  jmcneill 		*bus_start = __SHIFTOUT(res1, GET_SEG_INFO_BUS_START);
    117  1.1  jmcneill 		*bus_end = __SHIFTOUT(res1, GET_SEG_INFO_BUS_END);
    118  1.1  jmcneill 		*next_seg = (uint16_t)res2;
    119  1.1  jmcneill 	}
    120  1.1  jmcneill 
    121  1.1  jmcneill 	return status;
    122  1.1  jmcneill }
    123