The Pedigree Project  0.1
Mac.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 "Mac.h"
21 #include "pedigree/kernel/Log.h"
22 #include "pedigree/kernel/machine/Device.h"
23 #include "pedigree/kernel/machine/openfirmware/Device.h"
24 #include "pedigree/kernel/machine/openfirmware/OpenFirmware.h"
25 #include "pedigree/kernel/machine/ppc_common/pci.h"
26 #include "pedigree/kernel/processor/Processor.h"
27 #include <machine/ppc_common/Heathrow.h>
28 #include <machine/ppc_common/OpenPic.h>
29 
30 extern size_t resolveInterruptNumber(Device *pDev);
31 
32 Mac Mac::m_Instance;
33 
34 Machine &Machine::instance()
35 {
36  return Mac::instance();
37 }
38 
40 {
41  m_Vga.initialise();
42  m_Keyboard.initialise();
43  m_Decrementer.initialise();
44  m_bInitialised = true;
45 }
47 {
48  return &m_Serial[n];
49 }
51 {
52  return 1;
53 }
54 Vga *Mac::getVga(size_t n)
55 {
56  return &m_Vga;
57 }
59 {
60  return 1;
61 }
62 IrqManager *Mac::getIrqManager()
63 {
64  return m_pIrqManager;
65 }
67 {
68  return &m_Decrementer;
69 }
71 {
72  // TODO
73  return 0;
74 }
76 {
77  return &m_Keyboard;
78 }
79 
80 Mac::Mac() : m_Decrementer(), m_Vga(), m_Keyboard(), m_pIrqManager(0)
81 {
82 }
84 {
85 }
86 
87 // Performs a depth-first probe of the (OpenFirmware) device tree, populating
88 // the internal device tree.
89 static void probeDev(int depth, OFDevice *pOfDev, Device *pInternalDev)
90 {
91  OFHandle hChild = OpenFirmware::instance().getFirstChild(pOfDev);
92  while (hChild != 0)
93  {
94  // Convert the device handle into a real device object.
95  OFDevice dChild(hChild);
96  OFHandle hOldChild = hChild;
97  // Set hChild to now be a handle for the next device to probe.
98  hChild = OpenFirmware::instance().getSibling(&dChild);
99 
100  // Populate the new internal device node.
101  NormalStaticString type;
102  dChild.getProperty("device_type", type);
103 
104  // Prune stupid items.
105  if (type.length() == 0)
106  continue;
107 
108  // Create a new internal device node.
109  Device *node = new Device();
110  pInternalDev->addChild(node);
111  node->setParent(pInternalDev);
112 
113  node->setSpecificType(String(type));
114  node->setOFHandle(hOldChild);
115  node->setInterruptNumber(resolveInterruptNumber(node));
116 
117  probeDev(depth + 1, &dChild, node);
118  }
119 }
120 
122 {
123  // Find the root devices.
124  OFDevice ofRoot(OpenFirmware::instance().findDevice("/"));
125  Device &internalRoot = Device::root();
126 
127  // Start the device probing - this is recursive and obviously starts at a
128  // depth of 0.
129  probeDev(0, &ofRoot, &internalRoot);
130 
131  initialisePci();
132  if (!OpenPic::instance().initialise())
133  {
134  // OpenPic not found - try heathrow.
136  {
137  ERROR("No IRQ manager found.");
138  }
139  else
140  {
141  m_pIrqManager = &Heathrow::instance();
142  }
143  }
144  else
145  {
146  m_pIrqManager = &OpenPic::instance();
147  }
148 }
Definition: Mac.h:33
virtual Vga * getVga(size_t n)
Definition: Mac.cc:54
static OpenFirmware & instance()
Definition: OpenFirmware.h:45
virtual void setInterruptNumber(uintptr_t n)
Definition: Device.h:268
virtual Timer * getTimer()
Definition: Mac.cc:70
Definition: String.h:49
Mac()
Definition: Mac.cc:80
virtual ~Mac()
Definition: Mac.cc:83
OFHandle getSibling(class OFDevice *pDev)
Definition: OpenFirmware.cc:52
Definition: Device.h:43
virtual void initialiseDeviceTree()
Definition: Mac.cc:121
virtual Serial * getSerial(size_t n)
Definition: Mac.cc:46
virtual size_t getNumSerial()
Definition: Mac.cc:50
virtual SchedulerTimer * getSchedulerTimer()
Definition: Mac.cc:66
virtual void setSpecificType(String str)
Definition: Device.h:174
void addChild(Device *pDevice)
Definition: Device.cc:127
static Device & root()
Definition: Device.h:356
void setParent(Device *p)
Definition: Device.h:154
virtual Keyboard * getKeyboard()
Definition: Mac.cc:75
OFHandle getFirstChild(class OFDevice *pDev)
Definition: OpenFirmware.cc:58
static OpenPic & instance()
Definition: OpenPic.h:61
virtual size_t getNumVga()
Definition: Mac.cc:58
#define ERROR(text)
Definition: Log.h:82
virtual void initialise()
Definition: Mac.cc:39
static Heathrow & instance()
Definition: Heathrow.h:36