Linux Device Driver ch09.pdf

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

ThisistheTitleoftheBook,eMatterEditionCopyright©2005O’Reilly&Associates,Inc.Allrightsreserved.235Chapter9CHAPTER9CommunicatingwithHardwareAlthoughplayingwithscullandsimilartoysisagoodintroductiontothesoftwareinterfaceofaLinuxdevicedriver,implementingarealdevicerequireshardware.Thedriveristheabstractionlayerbetweensoftwareconceptsandhardwarecircuitry;assuch,itneedstotalkwithbothofthem.Upuntilnow,wehaveexaminedtheinter-nalsofsoftwareconcepts;thischaptercompletesthepicturebyshowingyouhowadrivercanaccessI/OportsandI/OmemorywhilebeingportableacrossLinuxplatforms.Thischaptercontinuesinthetraditionofstayingasindependentofspecifichard-wareaspossible.However,wherespecificexamplesareneeded,weusesimpledigi-talI/Oports(suchasthestandardPCparallelport)toshowhowtheI/Oinstructionsworkandnormalframe-buffervideomemorytoshowmemory-mappedI/O.WechosesimpledigitalI/Obecauseitistheeasiestformofaninput/outputport.Also,theparallelportimplementsrawI/Oandisavailableinmostcomputers:databitswrittentothedeviceappearontheoutputpins,andvoltagelevelsontheinputpinsaredirectlyaccessiblebytheprocessor.Inpractice,youhavetoconnectLEDsoraprintertotheporttoactuallyseetheresultsofadigitalI/Ooperation,buttheunderlyinghardwareisextremelyeasytouse.I/OPortsandI/OMemoryEveryperipheraldeviceiscontrolledbywritingandreadingitsregisters.Mostofthetimeadevicehasseveralregisters,andtheyareaccessedatconsecutiveaddresses,eitherinthememoryaddressspaceorintheI/Oaddressspace.Atthehardwarelevel,thereisnoconceptualdifferencebetweenmemoryregionsandI/Oregions:bothofthemareaccessedbyassertingelectricalsignalsontheaddress,ch09.10715Page235Friday,January21,200510:51AMThisistheTitleoftheBook,eMatterEditionCopyright©2005O’Reilly&Associates,Inc.Allrightsreserved.236|Chapter9:CommunicatingwithHardwarebusandcontrolbus(i.e.,thereadandwritesignals)*andbyreadingfromorwritingtothedatabus.WhilesomeCPUmanufacturersimplementasingleaddressspaceintheirchips,oth-ersdecidedthatperipheraldevicesaredifferentfrommemoryand,therefore,deserveaseparateaddressspace.Someprocessors(mostnotablythex86family)havesepa-ratereadandwriteelectricallinesforI/OportsandspecialCPUinstructionstoaccessports.Becauseperipheraldevicesarebuilttofitaperipheralbus,andthemostpopularI/Obusesaremodeledonthepersonalcomputer,evenprocessorsthatdonothaveasep-arateaddressspaceforI/OportsmustfakereadingandwritingI/Oportswhenaccessingsomeperipheraldevices,usuallybymeansofexternalchipsetsorextracir-cuitryintheCPUcore.Thelattersolutioniscommonwithintinyprocessorsmeantforembeddeduse.Forthesamereason,LinuximplementstheconceptofI/Oportsonallcomputerplatformsitrunson,evenonplatformswheretheCPUimplementsasingleaddressspace.Theimplementationofportaccesssometimesdependsonthespecificmakeandmodelofthehostcomputer(becausedifferentmodelsusedifferentchipsetstomapbustransactionsintomemoryaddressspace).EveniftheperipheralbushasaseparateaddressspaceforI/Oports,notalldevicesmaptheirregisterstoI/Oports.WhileuseofI/OportsiscommonforISAperiph-eralboards,mostPCIdevicesmapregistersintoamemoryaddressregion.ThisI/Omemoryapproachisgenerallypreferred,becauseitdoesn’trequiretheuseofspecial-purposeprocessorinstructions;CPUcoresaccessmemorymuchmoreefficiently,andthecompilerhasmuchmorefreedominregisterallocationandaddressing-modeselectionwhenaccessingmemory.I/ORegistersandConventionalMemoryDespitethestrongsimilaritybetweenhardwareregistersandmemory,aprogram-meraccessingI/OregistersmustbecarefultoavoidbeingtrickedbyCPU(orcom-piler)optimizationsthatcanmodifytheexpectedI/Obehavior.ThemaindifferencebetweenI/OregistersandRAMisthatI/Ooperationshavesideeffects,whilememoryoperationshavenone:theonlyeffectofamemorywriteisstoringavaluetoalocation,andamemoryreadreturnsthelastvaluewrittenthere.BecausememoryaccessspeedissocriticaltoCPUperformance,theno-side-effectscasehasbeenoptimizedinseveralways:valuesarecachedandread/writeinstruc-tionsarereordered.*Notallcomputerplatformsuseareadandawritesignal;somehavedifferentmeanstoaddressexternalcir-cuits.Thedifferenceisirrelevantatsoftwarelevel,however,andwe’llassumeallhavereadandwritetosim-plifythediscussion.,ch09.10715Page236Friday,January21,200510:51AMThisistheTitleoftheBook,eMatterEditionCopyright©2005O’Reilly&Associates,Inc.Allrightsreserved.I/OPortsandI/OMemory|237ThecompilercancachedatavaluesintoCPUregisterswithoutwritingthemtomemory,andevenifitstoresthem,bothwriteandreadoperationscanoperateoncachememorywithouteverreachingphysicalRAM.Reorderingcanalsohappenbothatthecompilerlevelandatthehardwarelevel:oftenasequenceofinstructionscanbeexecutedmorequicklyifitisruninanorderdifferentfromthatwhichappearsintheprogramtext,forexample,topreventinterlocksintheRISCpipeline.OnCISCprocessors,operationsthattakeasignificantamountoftimecanbeexe-cutedconcurrentlywithother,quickerones.Theseoptimizationsaretransparentandbenignwhenappliedtoconventionalmem-ory(atleastonuniprocessorsystems),buttheycanbefataltocorrectI/Oopera-tions,becausetheyinterferewiththose“sideeffects”thatarethemainreasonwhyadriveraccessesI/Oregisters.Theprocessorcannotanti

1 / 23
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功