| 1 | |
| 2 |
|
| 3 | |
| 4 |
|
| 5 | use super::Device;
|
| 6 | use crate::{
|
| 7 | bindings,
|
| 8 | device,
|
| 9 | device::Bound,
|
| 10 | error::to_result,
|
| 11 | irq::IrqRequest,
|
| 12 | prelude::*, |
| 13 | };
|
| 14 | use core::num::NonZero;
|
| 15 |
|
| 16 | |
| 17 | #[derive(Debug, Clone, Copy)]
|
| 18 | pub enum IrqType {
|
| 19 | |
| 20 | Intx,
|
| 21 | |
| 22 | Msi,
|
| 23 | |
| 24 | MsiX,
|
| 25 | }
|
| 26 |
|
| 27 | impl IrqType {
|
| 28 | |
| 29 | const fn as_raw(self) -> u32 {
|
| 30 | match self {
|
| 31 | IrqType::Intx => bindings::PCI_IRQ_INTX,
|
| 32 | IrqType::Msi => bindings::PCI_IRQ_MSI,
|
| 33 | IrqType::MsiX => bindings::PCI_IRQ_MSIX,
|
| 34 | }
|
| 35 | }
|
| 36 |
|
| 37 | |
| 38 | #[inline]
|
| 39 | const fn from_raw(raw: u32) -> Self {
|
| 40 | match raw {
|
| 41 | bindings::PCI_IRQ_MSIX => IrqType::MsiX,
|
| 42 | bindings::PCI_IRQ_MSI => IrqType::Msi,
|
| 43 | _ => IrqType::Intx,
|
| 44 | }
|
| 45 | }
|
| 46 | }
|
| 47 |
|
| 48 | |
| 49 | #[derive(Debug, Clone, Copy, Default)]
|
| 50 | pub struct IrqTypes(u32);
|
| 51 |
|
| 52 | impl IrqTypes {
|
| 53 | |
| 54 | pub const fn all() -> Self {
|
| 55 | Self(bindings::PCI_IRQ_ALL_TYPES)
|
| 56 | }
|
| 57 |
|
| 58 | |
| 59 | |
| 60 | |
| 61 | |
| 62 | |
| 63 | |
| 64 | |
| 65 | |
| 66 | |
| 67 | |
| 68 | pub const fn with(self, irq_type: IrqType) -> Self {
|
| 69 | Self(self.0 | irq_type.as_raw())
|
| 70 | }
|
| 71 |
|
| 72 | |
| 73 | const fn as_raw(self) -> u32 {
|
| 74 | self.0
|
| 75 | }
|
| 76 | }
|
| 77 |
|
| 78 | |
| 79 | |
| 80 | |
| 81 | |
| 82 | pub struct IrqVector<'a> {
|
| 83 | request: IrqRequest<'a>,
|
| 84 | reg: &'a IrqVectorRegistration<'a>,
|
| 85 | }
|
| 86 |
|
| 87 | impl<'a> IrqVector<'a> {
|
| 88 | |
| 89 | |
| 90 | |
| 91 | |
| 92 | |
| 93 | #[inline]
|
| 94 | unsafe fn new(request: IrqRequest<'a>, reg: &'a IrqVectorRegistration<'a>) -> Self {
|
| 95 | Self { request, reg }
|
| 96 | }
|
| 97 |
|
| 98 | |
| 99 | #[inline]
|
| 100 | pub fn vectors(&self) -> &'a IrqVectorRegistration<'a> {
|
| 101 | self.reg
|
| 102 | }
|
| 103 |
|
| 104 | |
| 105 | #[inline]
|
| 106 | pub fn irq_type(&self) -> IrqType {
|
| 107 | self.reg.irq_type()
|
| 108 | }
|
| 109 | }
|
| 110 |
|
| 111 | impl<'a> From<IrqVector<'a>> for IrqRequest<'a> {
|
| 112 | #[inline]
|
| 113 | fn from(vector: IrqVector<'a>) -> Self {
|
| 114 | vector.request
|
| 115 | }
|
| 116 | }
|
| 117 |
|
| 118 | |
| 119 | |
| 120 | |
| 121 | |
| 122 | |
| 123 | |
| 124 | |
| 125 | |
| 126 | pub struct IrqVectorRegistration<'a> {
|
| 127 | dev: &'a Device<Bound>,
|
| 128 | len: NonZero<usize>,
|
| 129 | }
|
| 130 |
|
| 131 | impl<'a> IrqVectorRegistration<'a> {
|
| 132 | |
| 133 | |
| 134 | |
| 135 | #[inline]
|
| 136 | #[allow(clippy::len_without_is_empty)]
|
| 137 | pub fn len(&self) -> usize {
|
| 138 | self.len.get()
|
| 139 | }
|
| 140 |
|
| 141 | |
| 142 | #[inline]
|
| 143 | pub fn irq_type(&self) -> IrqType {
|
| 144 | |
| 145 | IrqType::from_raw(unsafe { bindings::pci_irq_type(self.dev.as_raw()) })
|
| 146 | }
|
| 147 |
|
| 148 | |
| 149 | |
| 150 | |
| 151 | |
| 152 | #[inline]
|
| 153 | pub fn index(&self, index: usize) -> Result<IrqVector<'_>> {
|
| 154 | let index = u32::try_from(index)?;
|
| 155 |
|
| 156 | |
| 157 | let irq = unsafe { bindings::pci_irq_vector(self.dev.as_raw(), index) };
|
| 158 | if irq < 0 {
|
| 159 | return Err(Error::from_errno(irq));
|
| 160 | }
|
| 161 |
|
| 162 | |
| 163 | Ok(unsafe { IrqVector::new(IrqRequest::new(self.dev.as_ref(), irq as u32), self) })
|
| 164 | }
|
| 165 | }
|
| 166 |
|
| 167 | impl Drop for IrqVectorRegistration<'_> {
|
| 168 | #[inline]
|
| 169 | fn drop(&mut self) {
|
| 170 | |
| 171 | |
| 172 | unsafe { bindings::pci_free_irq_vectors(self.dev.as_raw()) };
|
| 173 | }
|
| 174 | }
|
| 175 |
|
| 176 | impl Device<device::Bound> {
|
| 177 | |
| 178 | |
| 179 | |
| 180 | |
| 181 | |
| 182 | |
| 183 | |
| 184 | |
| 185 | |
| 186 | |
| 187 | |
| 188 | |
| 189 | |
| 190 | |
| 191 | |
| 192 | |
| 193 | |
| 194 | |
| 195 | |
| 196 | |
| 197 | |
| 198 | |
| 199 | |
| 200 | |
| 201 | |
| 202 | |
| 203 | |
| 204 | |
| 205 | |
| 206 | |
| 207 | |
| 208 | |
| 209 | |
| 210 | |
| 211 | |
| 212 | |
| 213 | |
| 214 | |
| 215 | pub fn alloc_irq_vectors(
|
| 216 | &self,
|
| 217 | min_vecs: u32,
|
| 218 | max_vecs: u32,
|
| 219 | irq_types: IrqTypes,
|
| 220 | ) -> Result<IrqVectorRegistration<'_>> {
|
| 221 | |
| 222 | |
| 223 | |
| 224 | |
| 225 | |
| 226 | let ret = unsafe {
|
| 227 | bindings::pci_alloc_irq_vectors(self.as_raw(), min_vecs, max_vecs, irq_types.as_raw())
|
| 228 | };
|
| 229 | to_result(ret)?;
|
| 230 |
|
| 231 | let len = NonZero::new(ret as usize).ok_or(EINVAL)?;
|
| 232 |
|
| 233 | |
| 234 | Ok(IrqVectorRegistration { dev: self, len })
|
| 235 | }
|
| 236 | }
|