hi in today's video I want to show you how to implement a custom qmu PCI device so we will write a PCI device which will be emulated by qmu and the goal for today is when we're booting up our custom Linux kernel with our root file system we want to be able to detect this custom qumu device luckily I hadn't figured everything out by myself but I found a very good template so here I found this article on LinkedIn describing how to implement such a qumu PCI device and down here you can also see that
the source code is also available in a git repository so this is a git repository and here we can find the code for the qmu device a Linux driver for it and also a userspace application to interface the driver okay so I already prepared a little bit so let me navigate into the folder of our local copy of the qmu source code and in here under Hardware MK um PCI Echo device I've already created a small V PCI Echo device so the name of our device will be PCI Echo device the reason for this is
because bar one will work as an yeah as an storage where we can write data to it and then later read it back basically the device will have two region or two bars so bar zero is 60 16 bytes in size and bar one is for kilobytes in size bar one will be used for just storing the data we right into it and B zero um has some functionality so B zero contains of 16 bytes the first for bytes contains an ID so an ID we hard code into the register at address four we will
have our we have a invert register so when we are writing a value to it and read it back we will get back the inverted value then we have an irq register so this register um later we will add irq support to for this card and the irq register controls them so we can fire our irqs and acknowledge them and then we have a random value register every time we read from this register we will get the random value and the way so every bar is in memory region which is defined here so we have
to um map the memory region with some operations so a little bit down here we should be able to see this um memory region operations and here we are specifying which function is executed on a read which function is executed on a right what's the endin and what is the allowed access width so here for Paro only four byte access T Es are valid and let me show you what we're doing here in the read function so the read function has the following arguments this is a poter to the device we're reading from which we
are converting or which is our PCI Echo device then we will have a hardware address of the offset of the bar from which we want to read and we have a size and the return value is the value we read back so here from the read the only thing we have to do is in case the address is our random value register we're returning a random value else we're returning just um yeah element address divided by four from boss Zero and so we're getting back the value write is similar so we have our device pointer
we have the address and we have then we have the value you want to write and the size of the right access and what we're doing here so the ID register IQ register and random value Reg register are currently read only so nothing happens when we want to write to them only if we have the invert or the invert register um in this address then we will save the inverted value in our boss array okay but for bar one we have to implement the read in the right so this will be just a storage and
we should be able to access the bars with uh one bite or 8bit 16bit 32bit and 64bit accesses so here in our device data struct we have uh an array with four kilobytes and now we have to access them with the correct size so the way we are doing it here in the read if um the size is equal to one so a one by access then everything is easy because all we have to do is we have to return um PCI ech Echo device bar one on the address address and we are done this
will return an integer an 8bit value and we're done but else if the size is equal to 2 by then we will need a pointer from the type you in six s 60 16 byte pointer a bit pointer I will call it pointer and I will set it to un 60t um this address here yeah and then we can return our pointer and basically the same has to be done for the other two access wids so in case the size is four we have a 42 pointer or here 64 if the size is8 okay so
so much for the read now let's implement the but therefore I can just copy these um if as paths so we don't need a return here but this is equal to U 8T well yeah and down here we can change it to um the value of the pointer will be un n6t well and here we have 64 32 16 perfect now it's look now it's looking good okay and then when we scroll down a little bit here again we have our memory region operations here we have a realize function so in this realized function we
are initializing the device so here we are enabling or we are setting an interrupt pin so later we need this when we're implementing our interrupts and down here we are um initializing our memory region so the memory region is part of our device struct and here are the operations with which or which we are going to overload the device for which we want to use this memory maap region and a number and the size in bytes so in this this case it's 16 bytes for bar one and with PCI register bar we're registering our memory
iio region as PCI Base address base memory bar zero for our PCI device and that's basically it here it will be also possible to pass IO space but no we will stay with memory space here and basically down here we're doing the same for bar um for bar one so this is our memory region here are our operations a name and the size and bytes and down here we're registering it and that's basically it then we have an empty un init and instance init function and we have this class init function in this class init
function we're initializing our config space so here for example we are assigning the vendor ID the device ID which will be beef in this example the Clause revision and the class ID and down here this function adds our device to um yeah to qmu so it's available so here we're def uh defining our interfaces and interface methods here we can see we are overloading Pointers to our functions we have implemented earlier and here we were making it known to U and that's basically it okay so now what we have to do is we have to
um add this file so it will be compiled and therefore I will go to Hardware misk K config and here we have this PCI test device and I will use this as a template so I will copy this in here and instead of test I have a echo device now so this should be built into qumu if test device is set and it depends on PCI so we need PCI support for this then we also have to edit mason. buil and once again let's search for the PCI device and and when PCI echod def is
true then we will add the file PCI echod def. C here okay cool so now let's try to build it the problem is because we have changed the configuration we have to rebuild the whole qumu because we will now read the new configuration and then it will build everything uh I have an arrow here this is fast oh okay oh yes one yes so the the problem here is in this read function if I if none of these conditions is true I don't return anything so let me return FF FF FF FF FF FF FF
L here okay so let's from make again okay okay we don't have to recompile everything because I tested this before and the configuration wasn't changed because yeah from the last compilation nothing changed but if you are doing it with your build it will recompile everything cool and now if we start qmu system arm and list the device help we can see all the available devices and if we are geping for Echo we can see our PCI Echo device here for the PCI bus cool and now I want to start up our Linux kernel with our
root file system and search for the device but therefore I will make one minor change in the root file system so let me navigate to the root file system and in here I will create a new folder called called Etc init D and in here I will create the file Etc init D RS R CS so this is the init file which will be automatically called by byy box before starting byy box and what are what we are going to do here is we will Mount our system file system to/ sis and we will Mount
our proc file system to proc so at least the system file system is needed for LS PCI to read out the PCI devices which are available on the bus and we have to make this executable okay so now let's zip it and let's CD back to our build okay so now let me call um yeah kmu system arm our machine is ver minus 2. 10 because this machine has PCI support built into it this here is our kernel image our root file system our kernel command line no graphics and now we will add the device
PCI Echo device here so let's start this okay it's looking good we have a console and up here we can see some PCI devices which have been discovered and here we have one with the device number Beef and vendor number one 234 which is a qmu vendor number so this here should be our device and if we execute lspci we can see our custom device here popping up to cool the problem now is this um LS PCI which is included in busy box is very limited so in the next video we are going to build
the complete lspci to see more informations about our custom device so I guess that's it for today I hope you've enjoyed the video and learned something in case you want to support my work you can buy me a coffee on buy me coffee.com Linux so that's it for today thanks for watching and goodbye