1 use alloc::{string::ToString, sync::Arc}; 2 use system_error::SystemError; 3 use unified_init::macros::unified_init; 4 5 use crate::{ 6 driver::base::{ 7 kobject::{KObjType, KObject, KObjectManager, KObjectSysFSOps}, 8 kset::KSet, 9 }, 10 filesystem::{ 11 sysfs::{ 12 file::sysfs_emit_str, Attribute, AttributeGroup, SysFSOps, SysFSOpsSupport, 13 SYSFS_ATTR_MODE_RO, 14 }, 15 vfs::syscall::ModeType, 16 }, 17 init::initcall::INITCALL_POSTCORE, 18 misc::ksysfs::sys_kernel_kset, 19 }; 20 21 use super::{ 22 irqdesc::{irq_desc_manager, IrqDesc}, 23 IrqNumber, 24 }; 25 26 /// `/sys/kernel/irq`的kset 27 static mut SYS_KERNEL_IRQ_KSET_INSTANCE: Option<Arc<KSet>> = None; 28 29 #[inline(always)] 30 #[allow(dead_code)] 31 pub fn sys_kernel_irq_kset() -> Arc<KSet> { 32 unsafe { SYS_KERNEL_IRQ_KSET_INSTANCE.clone().unwrap() } 33 } 34 35 /// 中断描述符的kobjtype 36 /// 37 /// https://code.dragonos.org.cn/xref/linux-6.1.9/kernel/irq/irqdesc.c#280 38 #[derive(Debug)] 39 pub(super) struct IrqKObjType; 40 41 impl KObjType for IrqKObjType { 42 fn sysfs_ops(&self) -> Option<&dyn SysFSOps> { 43 Some(&KObjectSysFSOps) 44 } 45 46 fn attribute_groups(&self) -> Option<&'static [&'static dyn AttributeGroup]> { 47 Some(&[&IrqAttrGroup]) 48 } 49 50 fn release(&self, _kobj: Arc<dyn KObject>) { 51 52 // https://code.dragonos.org.cn/xref/linux-6.1.9/kernel/irq/irqdesc.c#428 53 } 54 } 55 56 #[derive(Debug)] 57 struct IrqAttrGroup; 58 59 impl AttributeGroup for IrqAttrGroup { 60 fn name(&self) -> Option<&str> { 61 None 62 } 63 64 /// 所有的属性 65 /// 66 /// 参考 https://code.dragonos.org.cn/xref/linux-6.1.9/kernel/irq/irqdesc.c#268 67 fn attrs(&self) -> &[&'static dyn Attribute] { 68 // 每个irq的属性 69 // todo: 添加per_cpu_count属性 70 &[ 71 &AttrChipName, 72 &AttrHardwareIrq, 73 &AttrType, 74 &AttrWakeup, 75 &AttrName, 76 &AttrActions, 77 ] 78 } 79 80 fn is_visible( 81 &self, 82 _kobj: Arc<dyn KObject>, 83 attr: &'static dyn Attribute, 84 ) -> Option<ModeType> { 85 Some(attr.mode()) 86 } 87 } 88 89 /// 初始化irq模块在sysfs中的目录 90 /// 91 /// 参考 https://code.dragonos.org.cn/xref/linux-6.1.9/kernel/irq/irqdesc.c#313 92 #[unified_init(INITCALL_POSTCORE)] 93 fn irq_sysfs_init() -> Result<(), SystemError> { 94 // todo!("irq_sysfs_init"); 95 96 let irq_kset = KSet::new("irq".to_string()); 97 irq_kset 98 .register(Some(sys_kernel_kset())) 99 .expect("register irq kset failed"); 100 unsafe { 101 SYS_KERNEL_IRQ_KSET_INSTANCE = Some(irq_kset); 102 } 103 104 // 把所有的irq都注册到/sys/kernel/irq下 105 for (irq, desc) in irq_desc_manager().iter_descs() { 106 irq_sysfs_add(irq, desc); 107 } 108 109 return Ok(()); 110 } 111 112 /// 把irqdesc添加到sysfs 113 fn irq_sysfs_add(irq: &IrqNumber, desc: &Arc<IrqDesc>) { 114 if unsafe { SYS_KERNEL_IRQ_KSET_INSTANCE.is_none() } { 115 return; 116 } 117 118 let kset = sys_kernel_irq_kset(); 119 KObjectManager::add_kobj(desc.clone() as Arc<dyn KObject>, Some(kset)).unwrap_or_else(|e| { 120 kwarn!("Failed to add irq({irq:?}) kobject to sysfs: {:?}", e); 121 }); 122 123 desc.mark_in_sysfs(); 124 } 125 126 /// 从sysfs中删除irqdesc 127 #[allow(dead_code)] 128 pub(super) fn irq_sysfs_del(desc: &Arc<IrqDesc>) { 129 if desc.in_sysfs() { 130 KObjectManager::remove_kobj(desc.clone() as Arc<dyn KObject>); 131 desc.mark_not_in_sysfs(); 132 } 133 } 134 #[derive(Debug)] 135 struct AttrChipName; 136 137 impl Attribute for AttrChipName { 138 fn name(&self) -> &str { 139 "chip_name" 140 } 141 142 fn mode(&self) -> ModeType { 143 SYSFS_ATTR_MODE_RO 144 } 145 146 fn support(&self) -> SysFSOpsSupport { 147 SysFSOpsSupport::ATTR_SHOW 148 } 149 150 fn show(&self, kobj: Arc<dyn KObject>, buf: &mut [u8]) -> Result<usize, SystemError> { 151 let irq_desc = kobj 152 .arc_any() 153 .downcast::<IrqDesc>() 154 .map_err(|_| SystemError::EINVAL)?; 155 let chip = irq_desc.irq_data().chip(); 156 let name = chip.name(); 157 let len = core::cmp::min(name.len() + 1, buf.len()); 158 let name = format!("{}\n", name); 159 buf[..len].copy_from_slice(name.as_bytes()); 160 return Ok(len); 161 } 162 } 163 164 #[derive(Debug)] 165 struct AttrHardwareIrq; 166 167 impl Attribute for AttrHardwareIrq { 168 fn name(&self) -> &str { 169 "hwirq" 170 } 171 172 fn mode(&self) -> ModeType { 173 SYSFS_ATTR_MODE_RO 174 } 175 176 fn support(&self) -> SysFSOpsSupport { 177 SysFSOpsSupport::ATTR_SHOW 178 } 179 180 fn show(&self, kobj: Arc<dyn KObject>, buf: &mut [u8]) -> Result<usize, SystemError> { 181 let irq_desc = kobj 182 .arc_any() 183 .downcast::<IrqDesc>() 184 .map_err(|_| SystemError::EINVAL)?; 185 let hwirq = irq_desc.hardware_irq(); 186 return sysfs_emit_str(buf, &format!("{}\n", hwirq.data())); 187 } 188 } 189 190 #[derive(Debug)] 191 struct AttrType; 192 193 impl Attribute for AttrType { 194 fn name(&self) -> &str { 195 "type" 196 } 197 198 fn mode(&self) -> ModeType { 199 SYSFS_ATTR_MODE_RO 200 } 201 202 fn support(&self) -> SysFSOpsSupport { 203 SysFSOpsSupport::ATTR_SHOW 204 } 205 206 fn show(&self, kobj: Arc<dyn KObject>, buf: &mut [u8]) -> Result<usize, SystemError> { 207 let irq_desc = kobj 208 .arc_any() 209 .downcast::<IrqDesc>() 210 .map_err(|_| SystemError::EINVAL)?; 211 let irq_type = if irq_desc.irq_data().is_level_type() { 212 "level" 213 } else { 214 "edge" 215 }; 216 217 return sysfs_emit_str(buf, &format!("{}\n", irq_type)); 218 } 219 } 220 221 #[derive(Debug)] 222 struct AttrWakeup; 223 224 impl Attribute for AttrWakeup { 225 fn name(&self) -> &str { 226 "wakeup" 227 } 228 229 fn mode(&self) -> ModeType { 230 SYSFS_ATTR_MODE_RO 231 } 232 233 fn support(&self) -> SysFSOpsSupport { 234 SysFSOpsSupport::ATTR_SHOW 235 } 236 237 fn show(&self, kobj: Arc<dyn KObject>, buf: &mut [u8]) -> Result<usize, SystemError> { 238 let irq_desc = kobj 239 .arc_any() 240 .downcast::<IrqDesc>() 241 .map_err(|_| SystemError::EINVAL)?; 242 let wakeup = irq_desc.irq_data().is_wakeup_set(); 243 return sysfs_emit_str(buf, &format!("{}\n", wakeup)); 244 } 245 } 246 247 #[derive(Debug)] 248 struct AttrName; 249 250 impl Attribute for AttrName { 251 fn name(&self) -> &str { 252 "name" 253 } 254 255 fn mode(&self) -> ModeType { 256 SYSFS_ATTR_MODE_RO 257 } 258 259 fn support(&self) -> SysFSOpsSupport { 260 SysFSOpsSupport::ATTR_SHOW 261 } 262 263 fn show(&self, kobj: Arc<dyn KObject>, buf: &mut [u8]) -> Result<usize, SystemError> { 264 let irq_desc = kobj 265 .arc_any() 266 .downcast::<IrqDesc>() 267 .map_err(|_| SystemError::EINVAL)?; 268 269 if let Some(name) = irq_desc.name() { 270 return sysfs_emit_str(buf, &format!("{}\n", name)); 271 } 272 273 return Ok(0); 274 } 275 } 276 277 #[derive(Debug)] 278 struct AttrActions; 279 280 impl Attribute for AttrActions { 281 fn name(&self) -> &str { 282 "actions" 283 } 284 285 fn mode(&self) -> ModeType { 286 SYSFS_ATTR_MODE_RO 287 } 288 289 fn support(&self) -> SysFSOpsSupport { 290 SysFSOpsSupport::ATTR_SHOW 291 } 292 293 fn show(&self, kobj: Arc<dyn KObject>, buf: &mut [u8]) -> Result<usize, SystemError> { 294 let irq_desc = kobj 295 .arc_any() 296 .downcast::<IrqDesc>() 297 .map_err(|_| SystemError::EINVAL)?; 298 299 let actions = irq_desc.actions(); 300 let mut len = 0; 301 302 for action in actions { 303 if len != 0 { 304 len += sysfs_emit_str(&mut buf[len..], &format!(",{}", action.name())).unwrap(); 305 } else { 306 len += sysfs_emit_str(&mut buf[len..], &format!("{}", action.name())).unwrap(); 307 } 308 309 if len >= buf.len() { 310 break; 311 } 312 } 313 314 if len != 0 && len < buf.len() { 315 len += sysfs_emit_str(&mut buf[len..], "\n").unwrap(); 316 } 317 318 return Ok(len); 319 } 320 } 321