The Pedigree Project  0.1
OpenFirmware.cc
1 /*
2  * Copyright (c) 2008-2014, Pedigree Developers
3  *
4  * Please see the CONTRIB file in the root of the source tree for a full
5  * list of contributors.
6  *
7  * Permission to use, copy, modify, and distribute this software for any
8  * purpose with or without fee is hereby granted, provided that the above
9  * copyright notice and this permission notice appear in all copies.
10  *
11  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18  */
19 
20 #include "pedigree/kernel/machine/openfirmware/OpenFirmware.h"
21 #include "pedigree/kernel/LockGuard.h"
22 #include "pedigree/kernel/Spinlock.h"
23 #include "pedigree/kernel/machine/openfirmware/Device.h"
24 
26 
27 OpenFirmware::OpenFirmware() : m_Interface(0)
28 {
29 }
30 
31 OpenFirmware::~OpenFirmware()
32 {
33 }
34 
35 void OpenFirmware::initialise(OFInterface interface)
36 {
37  m_Interface = interface;
38 
39  // Grab the current state of the SPRG registers.
40  asm volatile("mfsprg0 %0" : "=r"(m_Sprg0));
41  asm volatile("mfsprg1 %0" : "=r"(m_Sprg1));
42  asm volatile("mfsprg2 %0" : "=r"(m_Sprg2));
43  asm volatile("mfsprg3 %0" : "=r"(m_Sprg3));
44 }
45 
46 OFHandle OpenFirmware::findDevice(const char *pName)
47 {
48  return static_cast<OFHandle>(call(
49  "finddevice", 1, reinterpret_cast<OFParam>(const_cast<char *>(pName))));
50 }
51 
53 {
54  return static_cast<OFHandle>(
55  call("peer", 1, static_cast<OFHandle>(pDev->m_Handle)));
56 }
57 
59 {
60  return static_cast<OFHandle>(
61  call("child", 1, static_cast<OFHandle>(pDev->m_Handle)));
62 }
63 
65  const char *pService, int nArgs, OFParam p1, OFParam p2, OFParam p3,
66  OFParam p4, OFParam p5, OFParam p6, OFParam p7, OFParam p8)
67 {
68  static Spinlock lock;
69  LockGuard<Spinlock> guard(lock); // Grab a spinlock so that only one thing
70  // can call the prom at once.
71 
72  // Revert the state of the SPRG registers to how OF left them.
73  // These get mashed during our interrupt handlers, but OF needs them left
74  // alone (apparently)!
75  asm volatile("mtsprg0 %0" : : "r"(m_Sprg0));
76  asm volatile("mtsprg1 %0" : : "r"(m_Sprg1));
77  asm volatile("mtsprg2 %0" : : "r"(m_Sprg2));
78  asm volatile("mtsprg3 %0" : : "r"(m_Sprg3));
79 
80  PromArgs pa;
81  pa.service = pService;
82 
83  pa.args[0] = p1;
84  pa.args[1] = p2;
85  pa.args[2] = p3;
86  pa.args[3] = p4;
87  pa.args[4] = p5;
88  pa.args[5] = p6;
89  pa.args[6] = p7;
90  pa.args[7] = p8;
91  pa.args[8] = 0;
92  pa.args[9] = 0;
93  pa.nargs = nArgs;
94  pa.nret = 1;
95 
96  if (m_Interface(&pa) < 0)
97  return reinterpret_cast<OFParam>(-1);
98 
99  return pa.args[pa.nargs];
100 }
OFHandle findDevice(const char *pName)
Definition: OpenFirmware.cc:46
OFParam call(const char *pService, int nArgs, OFParam p1=0, OFParam p2=0, OFParam p3=0, OFParam p4=0, OFParam p5=0, OFParam p6=0, OFParam p7=0, OFParam p8=0)
Definition: OpenFirmware.cc:64
processor_register_t m_Sprg0
Definition: OpenFirmware.h:81
OFHandle getSibling(class OFDevice *pDev)
Definition: OpenFirmware.cc:52
void initialise(OFInterface interface)
Definition: OpenFirmware.cc:35
OFHandle getFirstChild(class OFDevice *pDev)
Definition: OpenFirmware.cc:58
OFInterface m_Interface
Definition: OpenFirmware.h:74
static OpenFirmware m_Instance
Definition: OpenFirmware.h:77