i915_gem.c 134 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134
  1. /*
  2. * Copyright © 2008 Intel Corporation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice (including the next
  12. * paragraph) shall be included in all copies or substantial portions of the
  13. * Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  18. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  21. * IN THE SOFTWARE.
  22. *
  23. * Authors:
  24. * Eric Anholt <eric@anholt.net>
  25. *
  26. */
  27. #include "drmP.h"
  28. #include "drm.h"
  29. #include "i915_drm.h"
  30. #include "i915_drv.h"
  31. #include "i915_trace.h"
  32. #include "intel_drv.h"
  33. #include <linux/slab.h>
  34. #include <linux/swap.h>
  35. #include <linux/pci.h>
  36. static void i915_gem_object_flush_gpu_write_domain(struct drm_gem_object *obj);
  37. static void i915_gem_object_flush_gtt_write_domain(struct drm_gem_object *obj);
  38. static void i915_gem_object_flush_cpu_write_domain(struct drm_gem_object *obj);
  39. static int i915_gem_object_set_to_cpu_domain(struct drm_gem_object *obj,
  40. int write);
  41. static int i915_gem_object_set_cpu_read_domain_range(struct drm_gem_object *obj,
  42. uint64_t offset,
  43. uint64_t size);
  44. static void i915_gem_object_set_to_full_cpu_read_domain(struct drm_gem_object *obj);
  45. static int i915_gem_object_wait_rendering(struct drm_gem_object *obj);
  46. static int i915_gem_object_bind_to_gtt(struct drm_gem_object *obj,
  47. unsigned alignment);
  48. static void i915_gem_clear_fence_reg(struct drm_gem_object *obj);
  49. static int i915_gem_evict_something(struct drm_device *dev, int min_size);
  50. static int i915_gem_evict_from_inactive_list(struct drm_device *dev);
  51. static int i915_gem_phys_pwrite(struct drm_device *dev, struct drm_gem_object *obj,
  52. struct drm_i915_gem_pwrite *args,
  53. struct drm_file *file_priv);
  54. static LIST_HEAD(shrink_list);
  55. static DEFINE_SPINLOCK(shrink_list_lock);
  56. int i915_gem_do_init(struct drm_device *dev, unsigned long start,
  57. unsigned long end)
  58. {
  59. drm_i915_private_t *dev_priv = dev->dev_private;
  60. if (start >= end ||
  61. (start & (PAGE_SIZE - 1)) != 0 ||
  62. (end & (PAGE_SIZE - 1)) != 0) {
  63. return -EINVAL;
  64. }
  65. drm_mm_init(&dev_priv->mm.gtt_space, start,
  66. end - start);
  67. dev->gtt_total = (uint32_t) (end - start);
  68. return 0;
  69. }
  70. int
  71. i915_gem_init_ioctl(struct drm_device *dev, void *data,
  72. struct drm_file *file_priv)
  73. {
  74. struct drm_i915_gem_init *args = data;
  75. int ret;
  76. mutex_lock(&dev->struct_mutex);
  77. ret = i915_gem_do_init(dev, args->gtt_start, args->gtt_end);
  78. mutex_unlock(&dev->struct_mutex);
  79. return ret;
  80. }
  81. int
  82. i915_gem_get_aperture_ioctl(struct drm_device *dev, void *data,
  83. struct drm_file *file_priv)
  84. {
  85. struct drm_i915_gem_get_aperture *args = data;
  86. if (!(dev->driver->driver_features & DRIVER_GEM))
  87. return -ENODEV;
  88. args->aper_size = dev->gtt_total;
  89. args->aper_available_size = (args->aper_size -
  90. atomic_read(&dev->pin_memory));
  91. return 0;
  92. }
  93. /**
  94. * Creates a new mm object and returns a handle to it.
  95. */
  96. int
  97. i915_gem_create_ioctl(struct drm_device *dev, void *data,
  98. struct drm_file *file_priv)
  99. {
  100. struct drm_i915_gem_create *args = data;
  101. struct drm_gem_object *obj;
  102. int ret;
  103. u32 handle;
  104. args->size = roundup(args->size, PAGE_SIZE);
  105. /* Allocate the new object */
  106. obj = i915_gem_alloc_object(dev, args->size);
  107. if (obj == NULL)
  108. return -ENOMEM;
  109. ret = drm_gem_handle_create(file_priv, obj, &handle);
  110. drm_gem_object_handle_unreference_unlocked(obj);
  111. if (ret)
  112. return ret;
  113. args->handle = handle;
  114. return 0;
  115. }
  116. static inline int
  117. fast_shmem_read(struct page **pages,
  118. loff_t page_base, int page_offset,
  119. char __user *data,
  120. int length)
  121. {
  122. char __iomem *vaddr;
  123. int unwritten;
  124. vaddr = kmap_atomic(pages[page_base >> PAGE_SHIFT], KM_USER0);
  125. if (vaddr == NULL)
  126. return -ENOMEM;
  127. unwritten = __copy_to_user_inatomic(data, vaddr + page_offset, length);
  128. kunmap_atomic(vaddr, KM_USER0);
  129. if (unwritten)
  130. return -EFAULT;
  131. return 0;
  132. }
  133. static int i915_gem_object_needs_bit17_swizzle(struct drm_gem_object *obj)
  134. {
  135. drm_i915_private_t *dev_priv = obj->dev->dev_private;
  136. struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
  137. return dev_priv->mm.bit_6_swizzle_x == I915_BIT_6_SWIZZLE_9_10_17 &&
  138. obj_priv->tiling_mode != I915_TILING_NONE;
  139. }
  140. static inline void
  141. slow_shmem_copy(struct page *dst_page,
  142. int dst_offset,
  143. struct page *src_page,
  144. int src_offset,
  145. int length)
  146. {
  147. char *dst_vaddr, *src_vaddr;
  148. dst_vaddr = kmap(dst_page);
  149. src_vaddr = kmap(src_page);
  150. memcpy(dst_vaddr + dst_offset, src_vaddr + src_offset, length);
  151. kunmap(src_page);
  152. kunmap(dst_page);
  153. }
  154. static inline void
  155. slow_shmem_bit17_copy(struct page *gpu_page,
  156. int gpu_offset,
  157. struct page *cpu_page,
  158. int cpu_offset,
  159. int length,
  160. int is_read)
  161. {
  162. char *gpu_vaddr, *cpu_vaddr;
  163. /* Use the unswizzled path if this page isn't affected. */
  164. if ((page_to_phys(gpu_page) & (1 << 17)) == 0) {
  165. if (is_read)
  166. return slow_shmem_copy(cpu_page, cpu_offset,
  167. gpu_page, gpu_offset, length);
  168. else
  169. return slow_shmem_copy(gpu_page, gpu_offset,
  170. cpu_page, cpu_offset, length);
  171. }
  172. gpu_vaddr = kmap(gpu_page);
  173. cpu_vaddr = kmap(cpu_page);
  174. /* Copy the data, XORing A6 with A17 (1). The user already knows he's
  175. * XORing with the other bits (A9 for Y, A9 and A10 for X)
  176. */
  177. while (length > 0) {
  178. int cacheline_end = ALIGN(gpu_offset + 1, 64);
  179. int this_length = min(cacheline_end - gpu_offset, length);
  180. int swizzled_gpu_offset = gpu_offset ^ 64;
  181. if (is_read) {
  182. memcpy(cpu_vaddr + cpu_offset,
  183. gpu_vaddr + swizzled_gpu_offset,
  184. this_length);
  185. } else {
  186. memcpy(gpu_vaddr + swizzled_gpu_offset,
  187. cpu_vaddr + cpu_offset,
  188. this_length);
  189. }
  190. cpu_offset += this_length;
  191. gpu_offset += this_length;
  192. length -= this_length;
  193. }
  194. kunmap(cpu_page);
  195. kunmap(gpu_page);
  196. }
  197. /**
  198. * This is the fast shmem pread path, which attempts to copy_from_user directly
  199. * from the backing pages of the object to the user's address space. On a
  200. * fault, it fails so we can fall back to i915_gem_shmem_pwrite_slow().
  201. */
  202. static int
  203. i915_gem_shmem_pread_fast(struct drm_device *dev, struct drm_gem_object *obj,
  204. struct drm_i915_gem_pread *args,
  205. struct drm_file *file_priv)
  206. {
  207. struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
  208. ssize_t remain;
  209. loff_t offset, page_base;
  210. char __user *user_data;
  211. int page_offset, page_length;
  212. int ret;
  213. user_data = (char __user *) (uintptr_t) args->data_ptr;
  214. remain = args->size;
  215. mutex_lock(&dev->struct_mutex);
  216. ret = i915_gem_object_get_pages(obj, 0);
  217. if (ret != 0)
  218. goto fail_unlock;
  219. ret = i915_gem_object_set_cpu_read_domain_range(obj, args->offset,
  220. args->size);
  221. if (ret != 0)
  222. goto fail_put_pages;
  223. obj_priv = to_intel_bo(obj);
  224. offset = args->offset;
  225. while (remain > 0) {
  226. /* Operation in this page
  227. *
  228. * page_base = page offset within aperture
  229. * page_offset = offset within page
  230. * page_length = bytes to copy for this page
  231. */
  232. page_base = (offset & ~(PAGE_SIZE-1));
  233. page_offset = offset & (PAGE_SIZE-1);
  234. page_length = remain;
  235. if ((page_offset + remain) > PAGE_SIZE)
  236. page_length = PAGE_SIZE - page_offset;
  237. ret = fast_shmem_read(obj_priv->pages,
  238. page_base, page_offset,
  239. user_data, page_length);
  240. if (ret)
  241. goto fail_put_pages;
  242. remain -= page_length;
  243. user_data += page_length;
  244. offset += page_length;
  245. }
  246. fail_put_pages:
  247. i915_gem_object_put_pages(obj);
  248. fail_unlock:
  249. mutex_unlock(&dev->struct_mutex);
  250. return ret;
  251. }
  252. static int
  253. i915_gem_object_get_pages_or_evict(struct drm_gem_object *obj)
  254. {
  255. int ret;
  256. ret = i915_gem_object_get_pages(obj, __GFP_NORETRY | __GFP_NOWARN);
  257. /* If we've insufficient memory to map in the pages, attempt
  258. * to make some space by throwing out some old buffers.
  259. */
  260. if (ret == -ENOMEM) {
  261. struct drm_device *dev = obj->dev;
  262. ret = i915_gem_evict_something(dev, obj->size);
  263. if (ret)
  264. return ret;
  265. ret = i915_gem_object_get_pages(obj, 0);
  266. }
  267. return ret;
  268. }
  269. /**
  270. * This is the fallback shmem pread path, which allocates temporary storage
  271. * in kernel space to copy_to_user into outside of the struct_mutex, so we
  272. * can copy out of the object's backing pages while holding the struct mutex
  273. * and not take page faults.
  274. */
  275. static int
  276. i915_gem_shmem_pread_slow(struct drm_device *dev, struct drm_gem_object *obj,
  277. struct drm_i915_gem_pread *args,
  278. struct drm_file *file_priv)
  279. {
  280. struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
  281. struct mm_struct *mm = current->mm;
  282. struct page **user_pages;
  283. ssize_t remain;
  284. loff_t offset, pinned_pages, i;
  285. loff_t first_data_page, last_data_page, num_pages;
  286. int shmem_page_index, shmem_page_offset;
  287. int data_page_index, data_page_offset;
  288. int page_length;
  289. int ret;
  290. uint64_t data_ptr = args->data_ptr;
  291. int do_bit17_swizzling;
  292. remain = args->size;
  293. /* Pin the user pages containing the data. We can't fault while
  294. * holding the struct mutex, yet we want to hold it while
  295. * dereferencing the user data.
  296. */
  297. first_data_page = data_ptr / PAGE_SIZE;
  298. last_data_page = (data_ptr + args->size - 1) / PAGE_SIZE;
  299. num_pages = last_data_page - first_data_page + 1;
  300. user_pages = drm_calloc_large(num_pages, sizeof(struct page *));
  301. if (user_pages == NULL)
  302. return -ENOMEM;
  303. down_read(&mm->mmap_sem);
  304. pinned_pages = get_user_pages(current, mm, (uintptr_t)args->data_ptr,
  305. num_pages, 1, 0, user_pages, NULL);
  306. up_read(&mm->mmap_sem);
  307. if (pinned_pages < num_pages) {
  308. ret = -EFAULT;
  309. goto fail_put_user_pages;
  310. }
  311. do_bit17_swizzling = i915_gem_object_needs_bit17_swizzle(obj);
  312. mutex_lock(&dev->struct_mutex);
  313. ret = i915_gem_object_get_pages_or_evict(obj);
  314. if (ret)
  315. goto fail_unlock;
  316. ret = i915_gem_object_set_cpu_read_domain_range(obj, args->offset,
  317. args->size);
  318. if (ret != 0)
  319. goto fail_put_pages;
  320. obj_priv = to_intel_bo(obj);
  321. offset = args->offset;
  322. while (remain > 0) {
  323. /* Operation in this page
  324. *
  325. * shmem_page_index = page number within shmem file
  326. * shmem_page_offset = offset within page in shmem file
  327. * data_page_index = page number in get_user_pages return
  328. * data_page_offset = offset with data_page_index page.
  329. * page_length = bytes to copy for this page
  330. */
  331. shmem_page_index = offset / PAGE_SIZE;
  332. shmem_page_offset = offset & ~PAGE_MASK;
  333. data_page_index = data_ptr / PAGE_SIZE - first_data_page;
  334. data_page_offset = data_ptr & ~PAGE_MASK;
  335. page_length = remain;
  336. if ((shmem_page_offset + page_length) > PAGE_SIZE)
  337. page_length = PAGE_SIZE - shmem_page_offset;
  338. if ((data_page_offset + page_length) > PAGE_SIZE)
  339. page_length = PAGE_SIZE - data_page_offset;
  340. if (do_bit17_swizzling) {
  341. slow_shmem_bit17_copy(obj_priv->pages[shmem_page_index],
  342. shmem_page_offset,
  343. user_pages[data_page_index],
  344. data_page_offset,
  345. page_length,
  346. 1);
  347. } else {
  348. slow_shmem_copy(user_pages[data_page_index],
  349. data_page_offset,
  350. obj_priv->pages[shmem_page_index],
  351. shmem_page_offset,
  352. page_length);
  353. }
  354. remain -= page_length;
  355. data_ptr += page_length;
  356. offset += page_length;
  357. }
  358. fail_put_pages:
  359. i915_gem_object_put_pages(obj);
  360. fail_unlock:
  361. mutex_unlock(&dev->struct_mutex);
  362. fail_put_user_pages:
  363. for (i = 0; i < pinned_pages; i++) {
  364. SetPageDirty(user_pages[i]);
  365. page_cache_release(user_pages[i]);
  366. }
  367. drm_free_large(user_pages);
  368. return ret;
  369. }
  370. /**
  371. * Reads data from the object referenced by handle.
  372. *
  373. * On error, the contents of *data are undefined.
  374. */
  375. int
  376. i915_gem_pread_ioctl(struct drm_device *dev, void *data,
  377. struct drm_file *file_priv)
  378. {
  379. struct drm_i915_gem_pread *args = data;
  380. struct drm_gem_object *obj;
  381. struct drm_i915_gem_object *obj_priv;
  382. int ret;
  383. obj = drm_gem_object_lookup(dev, file_priv, args->handle);
  384. if (obj == NULL)
  385. return -EBADF;
  386. obj_priv = to_intel_bo(obj);
  387. /* Bounds check source.
  388. *
  389. * XXX: This could use review for overflow issues...
  390. */
  391. if (args->offset > obj->size || args->size > obj->size ||
  392. args->offset + args->size > obj->size) {
  393. drm_gem_object_unreference_unlocked(obj);
  394. return -EINVAL;
  395. }
  396. if (i915_gem_object_needs_bit17_swizzle(obj)) {
  397. ret = i915_gem_shmem_pread_slow(dev, obj, args, file_priv);
  398. } else {
  399. ret = i915_gem_shmem_pread_fast(dev, obj, args, file_priv);
  400. if (ret != 0)
  401. ret = i915_gem_shmem_pread_slow(dev, obj, args,
  402. file_priv);
  403. }
  404. drm_gem_object_unreference_unlocked(obj);
  405. return ret;
  406. }
  407. /* This is the fast write path which cannot handle
  408. * page faults in the source data
  409. */
  410. static inline int
  411. fast_user_write(struct io_mapping *mapping,
  412. loff_t page_base, int page_offset,
  413. char __user *user_data,
  414. int length)
  415. {
  416. char *vaddr_atomic;
  417. unsigned long unwritten;
  418. vaddr_atomic = io_mapping_map_atomic_wc(mapping, page_base);
  419. unwritten = __copy_from_user_inatomic_nocache(vaddr_atomic + page_offset,
  420. user_data, length);
  421. io_mapping_unmap_atomic(vaddr_atomic);
  422. if (unwritten)
  423. return -EFAULT;
  424. return 0;
  425. }
  426. /* Here's the write path which can sleep for
  427. * page faults
  428. */
  429. static inline void
  430. slow_kernel_write(struct io_mapping *mapping,
  431. loff_t gtt_base, int gtt_offset,
  432. struct page *user_page, int user_offset,
  433. int length)
  434. {
  435. char __iomem *dst_vaddr;
  436. char *src_vaddr;
  437. dst_vaddr = io_mapping_map_wc(mapping, gtt_base);
  438. src_vaddr = kmap(user_page);
  439. memcpy_toio(dst_vaddr + gtt_offset,
  440. src_vaddr + user_offset,
  441. length);
  442. kunmap(user_page);
  443. io_mapping_unmap(dst_vaddr);
  444. }
  445. static inline int
  446. fast_shmem_write(struct page **pages,
  447. loff_t page_base, int page_offset,
  448. char __user *data,
  449. int length)
  450. {
  451. char __iomem *vaddr;
  452. unsigned long unwritten;
  453. vaddr = kmap_atomic(pages[page_base >> PAGE_SHIFT], KM_USER0);
  454. if (vaddr == NULL)
  455. return -ENOMEM;
  456. unwritten = __copy_from_user_inatomic(vaddr + page_offset, data, length);
  457. kunmap_atomic(vaddr, KM_USER0);
  458. if (unwritten)
  459. return -EFAULT;
  460. return 0;
  461. }
  462. /**
  463. * This is the fast pwrite path, where we copy the data directly from the
  464. * user into the GTT, uncached.
  465. */
  466. static int
  467. i915_gem_gtt_pwrite_fast(struct drm_device *dev, struct drm_gem_object *obj,
  468. struct drm_i915_gem_pwrite *args,
  469. struct drm_file *file_priv)
  470. {
  471. struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
  472. drm_i915_private_t *dev_priv = dev->dev_private;
  473. ssize_t remain;
  474. loff_t offset, page_base;
  475. char __user *user_data;
  476. int page_offset, page_length;
  477. int ret;
  478. user_data = (char __user *) (uintptr_t) args->data_ptr;
  479. remain = args->size;
  480. if (!access_ok(VERIFY_READ, user_data, remain))
  481. return -EFAULT;
  482. mutex_lock(&dev->struct_mutex);
  483. ret = i915_gem_object_pin(obj, 0);
  484. if (ret) {
  485. mutex_unlock(&dev->struct_mutex);
  486. return ret;
  487. }
  488. ret = i915_gem_object_set_to_gtt_domain(obj, 1);
  489. if (ret)
  490. goto fail;
  491. obj_priv = to_intel_bo(obj);
  492. offset = obj_priv->gtt_offset + args->offset;
  493. while (remain > 0) {
  494. /* Operation in this page
  495. *
  496. * page_base = page offset within aperture
  497. * page_offset = offset within page
  498. * page_length = bytes to copy for this page
  499. */
  500. page_base = (offset & ~(PAGE_SIZE-1));
  501. page_offset = offset & (PAGE_SIZE-1);
  502. page_length = remain;
  503. if ((page_offset + remain) > PAGE_SIZE)
  504. page_length = PAGE_SIZE - page_offset;
  505. ret = fast_user_write (dev_priv->mm.gtt_mapping, page_base,
  506. page_offset, user_data, page_length);
  507. /* If we get a fault while copying data, then (presumably) our
  508. * source page isn't available. Return the error and we'll
  509. * retry in the slow path.
  510. */
  511. if (ret)
  512. goto fail;
  513. remain -= page_length;
  514. user_data += page_length;
  515. offset += page_length;
  516. }
  517. fail:
  518. i915_gem_object_unpin(obj);
  519. mutex_unlock(&dev->struct_mutex);
  520. return ret;
  521. }
  522. /**
  523. * This is the fallback GTT pwrite path, which uses get_user_pages to pin
  524. * the memory and maps it using kmap_atomic for copying.
  525. *
  526. * This code resulted in x11perf -rgb10text consuming about 10% more CPU
  527. * than using i915_gem_gtt_pwrite_fast on a G45 (32-bit).
  528. */
  529. static int
  530. i915_gem_gtt_pwrite_slow(struct drm_device *dev, struct drm_gem_object *obj,
  531. struct drm_i915_gem_pwrite *args,
  532. struct drm_file *file_priv)
  533. {
  534. struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
  535. drm_i915_private_t *dev_priv = dev->dev_private;
  536. ssize_t remain;
  537. loff_t gtt_page_base, offset;
  538. loff_t first_data_page, last_data_page, num_pages;
  539. loff_t pinned_pages, i;
  540. struct page **user_pages;
  541. struct mm_struct *mm = current->mm;
  542. int gtt_page_offset, data_page_offset, data_page_index, page_length;
  543. int ret;
  544. uint64_t data_ptr = args->data_ptr;
  545. remain = args->size;
  546. /* Pin the user pages containing the data. We can't fault while
  547. * holding the struct mutex, and all of the pwrite implementations
  548. * want to hold it while dereferencing the user data.
  549. */
  550. first_data_page = data_ptr / PAGE_SIZE;
  551. last_data_page = (data_ptr + args->size - 1) / PAGE_SIZE;
  552. num_pages = last_data_page - first_data_page + 1;
  553. user_pages = drm_calloc_large(num_pages, sizeof(struct page *));
  554. if (user_pages == NULL)
  555. return -ENOMEM;
  556. down_read(&mm->mmap_sem);
  557. pinned_pages = get_user_pages(current, mm, (uintptr_t)args->data_ptr,
  558. num_pages, 0, 0, user_pages, NULL);
  559. up_read(&mm->mmap_sem);
  560. if (pinned_pages < num_pages) {
  561. ret = -EFAULT;
  562. goto out_unpin_pages;
  563. }
  564. mutex_lock(&dev->struct_mutex);
  565. ret = i915_gem_object_pin(obj, 0);
  566. if (ret)
  567. goto out_unlock;
  568. ret = i915_gem_object_set_to_gtt_domain(obj, 1);
  569. if (ret)
  570. goto out_unpin_object;
  571. obj_priv = to_intel_bo(obj);
  572. offset = obj_priv->gtt_offset + args->offset;
  573. while (remain > 0) {
  574. /* Operation in this page
  575. *
  576. * gtt_page_base = page offset within aperture
  577. * gtt_page_offset = offset within page in aperture
  578. * data_page_index = page number in get_user_pages return
  579. * data_page_offset = offset with data_page_index page.
  580. * page_length = bytes to copy for this page
  581. */
  582. gtt_page_base = offset & PAGE_MASK;
  583. gtt_page_offset = offset & ~PAGE_MASK;
  584. data_page_index = data_ptr / PAGE_SIZE - first_data_page;
  585. data_page_offset = data_ptr & ~PAGE_MASK;
  586. page_length = remain;
  587. if ((gtt_page_offset + page_length) > PAGE_SIZE)
  588. page_length = PAGE_SIZE - gtt_page_offset;
  589. if ((data_page_offset + page_length) > PAGE_SIZE)
  590. page_length = PAGE_SIZE - data_page_offset;
  591. slow_kernel_write(dev_priv->mm.gtt_mapping,
  592. gtt_page_base, gtt_page_offset,
  593. user_pages[data_page_index],
  594. data_page_offset,
  595. page_length);
  596. remain -= page_length;
  597. offset += page_length;
  598. data_ptr += page_length;
  599. }
  600. out_unpin_object:
  601. i915_gem_object_unpin(obj);
  602. out_unlock:
  603. mutex_unlock(&dev->struct_mutex);
  604. out_unpin_pages:
  605. for (i = 0; i < pinned_pages; i++)
  606. page_cache_release(user_pages[i]);
  607. drm_free_large(user_pages);
  608. return ret;
  609. }
  610. /**
  611. * This is the fast shmem pwrite path, which attempts to directly
  612. * copy_from_user into the kmapped pages backing the object.
  613. */
  614. static int
  615. i915_gem_shmem_pwrite_fast(struct drm_device *dev, struct drm_gem_object *obj,
  616. struct drm_i915_gem_pwrite *args,
  617. struct drm_file *file_priv)
  618. {
  619. struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
  620. ssize_t remain;
  621. loff_t offset, page_base;
  622. char __user *user_data;
  623. int page_offset, page_length;
  624. int ret;
  625. user_data = (char __user *) (uintptr_t) args->data_ptr;
  626. remain = args->size;
  627. mutex_lock(&dev->struct_mutex);
  628. ret = i915_gem_object_get_pages(obj, 0);
  629. if (ret != 0)
  630. goto fail_unlock;
  631. ret = i915_gem_object_set_to_cpu_domain(obj, 1);
  632. if (ret != 0)
  633. goto fail_put_pages;
  634. obj_priv = to_intel_bo(obj);
  635. offset = args->offset;
  636. obj_priv->dirty = 1;
  637. while (remain > 0) {
  638. /* Operation in this page
  639. *
  640. * page_base = page offset within aperture
  641. * page_offset = offset within page
  642. * page_length = bytes to copy for this page
  643. */
  644. page_base = (offset & ~(PAGE_SIZE-1));
  645. page_offset = offset & (PAGE_SIZE-1);
  646. page_length = remain;
  647. if ((page_offset + remain) > PAGE_SIZE)
  648. page_length = PAGE_SIZE - page_offset;
  649. ret = fast_shmem_write(obj_priv->pages,
  650. page_base, page_offset,
  651. user_data, page_length);
  652. if (ret)
  653. goto fail_put_pages;
  654. remain -= page_length;
  655. user_data += page_length;
  656. offset += page_length;
  657. }
  658. fail_put_pages:
  659. i915_gem_object_put_pages(obj);
  660. fail_unlock:
  661. mutex_unlock(&dev->struct_mutex);
  662. return ret;
  663. }
  664. /**
  665. * This is the fallback shmem pwrite path, which uses get_user_pages to pin
  666. * the memory and maps it using kmap_atomic for copying.
  667. *
  668. * This avoids taking mmap_sem for faulting on the user's address while the
  669. * struct_mutex is held.
  670. */
  671. static int
  672. i915_gem_shmem_pwrite_slow(struct drm_device *dev, struct drm_gem_object *obj,
  673. struct drm_i915_gem_pwrite *args,
  674. struct drm_file *file_priv)
  675. {
  676. struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
  677. struct mm_struct *mm = current->mm;
  678. struct page **user_pages;
  679. ssize_t remain;
  680. loff_t offset, pinned_pages, i;
  681. loff_t first_data_page, last_data_page, num_pages;
  682. int shmem_page_index, shmem_page_offset;
  683. int data_page_index, data_page_offset;
  684. int page_length;
  685. int ret;
  686. uint64_t data_ptr = args->data_ptr;
  687. int do_bit17_swizzling;
  688. remain = args->size;
  689. /* Pin the user pages containing the data. We can't fault while
  690. * holding the struct mutex, and all of the pwrite implementations
  691. * want to hold it while dereferencing the user data.
  692. */
  693. first_data_page = data_ptr / PAGE_SIZE;
  694. last_data_page = (data_ptr + args->size - 1) / PAGE_SIZE;
  695. num_pages = last_data_page - first_data_page + 1;
  696. user_pages = drm_calloc_large(num_pages, sizeof(struct page *));
  697. if (user_pages == NULL)
  698. return -ENOMEM;
  699. down_read(&mm->mmap_sem);
  700. pinned_pages = get_user_pages(current, mm, (uintptr_t)args->data_ptr,
  701. num_pages, 0, 0, user_pages, NULL);
  702. up_read(&mm->mmap_sem);
  703. if (pinned_pages < num_pages) {
  704. ret = -EFAULT;
  705. goto fail_put_user_pages;
  706. }
  707. do_bit17_swizzling = i915_gem_object_needs_bit17_swizzle(obj);
  708. mutex_lock(&dev->struct_mutex);
  709. ret = i915_gem_object_get_pages_or_evict(obj);
  710. if (ret)
  711. goto fail_unlock;
  712. ret = i915_gem_object_set_to_cpu_domain(obj, 1);
  713. if (ret != 0)
  714. goto fail_put_pages;
  715. obj_priv = to_intel_bo(obj);
  716. offset = args->offset;
  717. obj_priv->dirty = 1;
  718. while (remain > 0) {
  719. /* Operation in this page
  720. *
  721. * shmem_page_index = page number within shmem file
  722. * shmem_page_offset = offset within page in shmem file
  723. * data_page_index = page number in get_user_pages return
  724. * data_page_offset = offset with data_page_index page.
  725. * page_length = bytes to copy for this page
  726. */
  727. shmem_page_index = offset / PAGE_SIZE;
  728. shmem_page_offset = offset & ~PAGE_MASK;
  729. data_page_index = data_ptr / PAGE_SIZE - first_data_page;
  730. data_page_offset = data_ptr & ~PAGE_MASK;
  731. page_length = remain;
  732. if ((shmem_page_offset + page_length) > PAGE_SIZE)
  733. page_length = PAGE_SIZE - shmem_page_offset;
  734. if ((data_page_offset + page_length) > PAGE_SIZE)
  735. page_length = PAGE_SIZE - data_page_offset;
  736. if (do_bit17_swizzling) {
  737. slow_shmem_bit17_copy(obj_priv->pages[shmem_page_index],
  738. shmem_page_offset,
  739. user_pages[data_page_index],
  740. data_page_offset,
  741. page_length,
  742. 0);
  743. } else {
  744. slow_shmem_copy(obj_priv->pages[shmem_page_index],
  745. shmem_page_offset,
  746. user_pages[data_page_index],
  747. data_page_offset,
  748. page_length);
  749. }
  750. remain -= page_length;
  751. data_ptr += page_length;
  752. offset += page_length;
  753. }
  754. fail_put_pages:
  755. i915_gem_object_put_pages(obj);
  756. fail_unlock:
  757. mutex_unlock(&dev->struct_mutex);
  758. fail_put_user_pages:
  759. for (i = 0; i < pinned_pages; i++)
  760. page_cache_release(user_pages[i]);
  761. drm_free_large(user_pages);
  762. return ret;
  763. }
  764. /**
  765. * Writes data to the object referenced by handle.
  766. *
  767. * On error, the contents of the buffer that were to be modified are undefined.
  768. */
  769. int
  770. i915_gem_pwrite_ioctl(struct drm_device *dev, void *data,
  771. struct drm_file *file_priv)
  772. {
  773. struct drm_i915_gem_pwrite *args = data;
  774. struct drm_gem_object *obj;
  775. struct drm_i915_gem_object *obj_priv;
  776. int ret = 0;
  777. obj = drm_gem_object_lookup(dev, file_priv, args->handle);
  778. if (obj == NULL)
  779. return -EBADF;
  780. obj_priv = to_intel_bo(obj);
  781. /* Bounds check destination.
  782. *
  783. * XXX: This could use review for overflow issues...
  784. */
  785. if (args->offset > obj->size || args->size > obj->size ||
  786. args->offset + args->size > obj->size) {
  787. drm_gem_object_unreference_unlocked(obj);
  788. return -EINVAL;
  789. }
  790. /* We can only do the GTT pwrite on untiled buffers, as otherwise
  791. * it would end up going through the fenced access, and we'll get
  792. * different detiling behavior between reading and writing.
  793. * pread/pwrite currently are reading and writing from the CPU
  794. * perspective, requiring manual detiling by the client.
  795. */
  796. if (obj_priv->phys_obj)
  797. ret = i915_gem_phys_pwrite(dev, obj, args, file_priv);
  798. else if (obj_priv->tiling_mode == I915_TILING_NONE &&
  799. dev->gtt_total != 0 &&
  800. obj->write_domain != I915_GEM_DOMAIN_CPU) {
  801. ret = i915_gem_gtt_pwrite_fast(dev, obj, args, file_priv);
  802. if (ret == -EFAULT) {
  803. ret = i915_gem_gtt_pwrite_slow(dev, obj, args,
  804. file_priv);
  805. }
  806. } else if (i915_gem_object_needs_bit17_swizzle(obj)) {
  807. ret = i915_gem_shmem_pwrite_slow(dev, obj, args, file_priv);
  808. } else {
  809. ret = i915_gem_shmem_pwrite_fast(dev, obj, args, file_priv);
  810. if (ret == -EFAULT) {
  811. ret = i915_gem_shmem_pwrite_slow(dev, obj, args,
  812. file_priv);
  813. }
  814. }
  815. #if WATCH_PWRITE
  816. if (ret)
  817. DRM_INFO("pwrite failed %d\n", ret);
  818. #endif
  819. drm_gem_object_unreference_unlocked(obj);
  820. return ret;
  821. }
  822. /**
  823. * Called when user space prepares to use an object with the CPU, either
  824. * through the mmap ioctl's mapping or a GTT mapping.
  825. */
  826. int
  827. i915_gem_set_domain_ioctl(struct drm_device *dev, void *data,
  828. struct drm_file *file_priv)
  829. {
  830. struct drm_i915_private *dev_priv = dev->dev_private;
  831. struct drm_i915_gem_set_domain *args = data;
  832. struct drm_gem_object *obj;
  833. struct drm_i915_gem_object *obj_priv;
  834. uint32_t read_domains = args->read_domains;
  835. uint32_t write_domain = args->write_domain;
  836. int ret;
  837. if (!(dev->driver->driver_features & DRIVER_GEM))
  838. return -ENODEV;
  839. /* Only handle setting domains to types used by the CPU. */
  840. if (write_domain & I915_GEM_GPU_DOMAINS)
  841. return -EINVAL;
  842. if (read_domains & I915_GEM_GPU_DOMAINS)
  843. return -EINVAL;
  844. /* Having something in the write domain implies it's in the read
  845. * domain, and only that read domain. Enforce that in the request.
  846. */
  847. if (write_domain != 0 && read_domains != write_domain)
  848. return -EINVAL;
  849. obj = drm_gem_object_lookup(dev, file_priv, args->handle);
  850. if (obj == NULL)
  851. return -EBADF;
  852. obj_priv = to_intel_bo(obj);
  853. mutex_lock(&dev->struct_mutex);
  854. intel_mark_busy(dev, obj);
  855. #if WATCH_BUF
  856. DRM_INFO("set_domain_ioctl %p(%zd), %08x %08x\n",
  857. obj, obj->size, read_domains, write_domain);
  858. #endif
  859. if (read_domains & I915_GEM_DOMAIN_GTT) {
  860. ret = i915_gem_object_set_to_gtt_domain(obj, write_domain != 0);
  861. /* Update the LRU on the fence for the CPU access that's
  862. * about to occur.
  863. */
  864. if (obj_priv->fence_reg != I915_FENCE_REG_NONE) {
  865. struct drm_i915_fence_reg *reg =
  866. &dev_priv->fence_regs[obj_priv->fence_reg];
  867. list_move_tail(&reg->lru_list,
  868. &dev_priv->mm.fence_list);
  869. }
  870. /* Silently promote "you're not bound, there was nothing to do"
  871. * to success, since the client was just asking us to
  872. * make sure everything was done.
  873. */
  874. if (ret == -EINVAL)
  875. ret = 0;
  876. } else {
  877. ret = i915_gem_object_set_to_cpu_domain(obj, write_domain != 0);
  878. }
  879. drm_gem_object_unreference(obj);
  880. mutex_unlock(&dev->struct_mutex);
  881. return ret;
  882. }
  883. /**
  884. * Called when user space has done writes to this buffer
  885. */
  886. int
  887. i915_gem_sw_finish_ioctl(struct drm_device *dev, void *data,
  888. struct drm_file *file_priv)
  889. {
  890. struct drm_i915_gem_sw_finish *args = data;
  891. struct drm_gem_object *obj;
  892. struct drm_i915_gem_object *obj_priv;
  893. int ret = 0;
  894. if (!(dev->driver->driver_features & DRIVER_GEM))
  895. return -ENODEV;
  896. mutex_lock(&dev->struct_mutex);
  897. obj = drm_gem_object_lookup(dev, file_priv, args->handle);
  898. if (obj == NULL) {
  899. mutex_unlock(&dev->struct_mutex);
  900. return -EBADF;
  901. }
  902. #if WATCH_BUF
  903. DRM_INFO("%s: sw_finish %d (%p %zd)\n",
  904. __func__, args->handle, obj, obj->size);
  905. #endif
  906. obj_priv = to_intel_bo(obj);
  907. /* Pinned buffers may be scanout, so flush the cache */
  908. if (obj_priv->pin_count)
  909. i915_gem_object_flush_cpu_write_domain(obj);
  910. drm_gem_object_unreference(obj);
  911. mutex_unlock(&dev->struct_mutex);
  912. return ret;
  913. }
  914. /**
  915. * Maps the contents of an object, returning the address it is mapped
  916. * into.
  917. *
  918. * While the mapping holds a reference on the contents of the object, it doesn't
  919. * imply a ref on the object itself.
  920. */
  921. int
  922. i915_gem_mmap_ioctl(struct drm_device *dev, void *data,
  923. struct drm_file *file_priv)
  924. {
  925. struct drm_i915_gem_mmap *args = data;
  926. struct drm_gem_object *obj;
  927. loff_t offset;
  928. unsigned long addr;
  929. if (!(dev->driver->driver_features & DRIVER_GEM))
  930. return -ENODEV;
  931. obj = drm_gem_object_lookup(dev, file_priv, args->handle);
  932. if (obj == NULL)
  933. return -EBADF;
  934. offset = args->offset;
  935. down_write(&current->mm->mmap_sem);
  936. addr = do_mmap(obj->filp, 0, args->size,
  937. PROT_READ | PROT_WRITE, MAP_SHARED,
  938. args->offset);
  939. up_write(&current->mm->mmap_sem);
  940. drm_gem_object_unreference_unlocked(obj);
  941. if (IS_ERR((void *)addr))
  942. return addr;
  943. args->addr_ptr = (uint64_t) addr;
  944. return 0;
  945. }
  946. /**
  947. * i915_gem_fault - fault a page into the GTT
  948. * vma: VMA in question
  949. * vmf: fault info
  950. *
  951. * The fault handler is set up by drm_gem_mmap() when a object is GTT mapped
  952. * from userspace. The fault handler takes care of binding the object to
  953. * the GTT (if needed), allocating and programming a fence register (again,
  954. * only if needed based on whether the old reg is still valid or the object
  955. * is tiled) and inserting a new PTE into the faulting process.
  956. *
  957. * Note that the faulting process may involve evicting existing objects
  958. * from the GTT and/or fence registers to make room. So performance may
  959. * suffer if the GTT working set is large or there are few fence registers
  960. * left.
  961. */
  962. int i915_gem_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
  963. {
  964. struct drm_gem_object *obj = vma->vm_private_data;
  965. struct drm_device *dev = obj->dev;
  966. struct drm_i915_private *dev_priv = dev->dev_private;
  967. struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
  968. pgoff_t page_offset;
  969. unsigned long pfn;
  970. int ret = 0;
  971. bool write = !!(vmf->flags & FAULT_FLAG_WRITE);
  972. /* We don't use vmf->pgoff since that has the fake offset */
  973. page_offset = ((unsigned long)vmf->virtual_address - vma->vm_start) >>
  974. PAGE_SHIFT;
  975. /* Now bind it into the GTT if needed */
  976. mutex_lock(&dev->struct_mutex);
  977. if (!obj_priv->gtt_space) {
  978. ret = i915_gem_object_bind_to_gtt(obj, 0);
  979. if (ret)
  980. goto unlock;
  981. list_add_tail(&obj_priv->list, &dev_priv->mm.inactive_list);
  982. ret = i915_gem_object_set_to_gtt_domain(obj, write);
  983. if (ret)
  984. goto unlock;
  985. }
  986. /* Need a new fence register? */
  987. if (obj_priv->tiling_mode != I915_TILING_NONE) {
  988. ret = i915_gem_object_get_fence_reg(obj);
  989. if (ret)
  990. goto unlock;
  991. }
  992. pfn = ((dev->agp->base + obj_priv->gtt_offset) >> PAGE_SHIFT) +
  993. page_offset;
  994. /* Finally, remap it using the new GTT offset */
  995. ret = vm_insert_pfn(vma, (unsigned long)vmf->virtual_address, pfn);
  996. unlock:
  997. mutex_unlock(&dev->struct_mutex);
  998. switch (ret) {
  999. case 0:
  1000. case -ERESTARTSYS:
  1001. return VM_FAULT_NOPAGE;
  1002. case -ENOMEM:
  1003. case -EAGAIN:
  1004. return VM_FAULT_OOM;
  1005. default:
  1006. return VM_FAULT_SIGBUS;
  1007. }
  1008. }
  1009. /**
  1010. * i915_gem_create_mmap_offset - create a fake mmap offset for an object
  1011. * @obj: obj in question
  1012. *
  1013. * GEM memory mapping works by handing back to userspace a fake mmap offset
  1014. * it can use in a subsequent mmap(2) call. The DRM core code then looks
  1015. * up the object based on the offset and sets up the various memory mapping
  1016. * structures.
  1017. *
  1018. * This routine allocates and attaches a fake offset for @obj.
  1019. */
  1020. static int
  1021. i915_gem_create_mmap_offset(struct drm_gem_object *obj)
  1022. {
  1023. struct drm_device *dev = obj->dev;
  1024. struct drm_gem_mm *mm = dev->mm_private;
  1025. struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
  1026. struct drm_map_list *list;
  1027. struct drm_local_map *map;
  1028. int ret = 0;
  1029. /* Set the object up for mmap'ing */
  1030. list = &obj->map_list;
  1031. list->map = kzalloc(sizeof(struct drm_map_list), GFP_KERNEL);
  1032. if (!list->map)
  1033. return -ENOMEM;
  1034. map = list->map;
  1035. map->type = _DRM_GEM;
  1036. map->size = obj->size;
  1037. map->handle = obj;
  1038. /* Get a DRM GEM mmap offset allocated... */
  1039. list->file_offset_node = drm_mm_search_free(&mm->offset_manager,
  1040. obj->size / PAGE_SIZE, 0, 0);
  1041. if (!list->file_offset_node) {
  1042. DRM_ERROR("failed to allocate offset for bo %d\n", obj->name);
  1043. ret = -ENOMEM;
  1044. goto out_free_list;
  1045. }
  1046. list->file_offset_node = drm_mm_get_block(list->file_offset_node,
  1047. obj->size / PAGE_SIZE, 0);
  1048. if (!list->file_offset_node) {
  1049. ret = -ENOMEM;
  1050. goto out_free_list;
  1051. }
  1052. list->hash.key = list->file_offset_node->start;
  1053. if (drm_ht_insert_item(&mm->offset_hash, &list->hash)) {
  1054. DRM_ERROR("failed to add to map hash\n");
  1055. ret = -ENOMEM;
  1056. goto out_free_mm;
  1057. }
  1058. /* By now we should be all set, any drm_mmap request on the offset
  1059. * below will get to our mmap & fault handler */
  1060. obj_priv->mmap_offset = ((uint64_t) list->hash.key) << PAGE_SHIFT;
  1061. return 0;
  1062. out_free_mm:
  1063. drm_mm_put_block(list->file_offset_node);
  1064. out_free_list:
  1065. kfree(list->map);
  1066. return ret;
  1067. }
  1068. /**
  1069. * i915_gem_release_mmap - remove physical page mappings
  1070. * @obj: obj in question
  1071. *
  1072. * Preserve the reservation of the mmapping with the DRM core code, but
  1073. * relinquish ownership of the pages back to the system.
  1074. *
  1075. * It is vital that we remove the page mapping if we have mapped a tiled
  1076. * object through the GTT and then lose the fence register due to
  1077. * resource pressure. Similarly if the object has been moved out of the
  1078. * aperture, than pages mapped into userspace must be revoked. Removing the
  1079. * mapping will then trigger a page fault on the next user access, allowing
  1080. * fixup by i915_gem_fault().
  1081. */
  1082. void
  1083. i915_gem_release_mmap(struct drm_gem_object *obj)
  1084. {
  1085. struct drm_device *dev = obj->dev;
  1086. struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
  1087. if (dev->dev_mapping)
  1088. unmap_mapping_range(dev->dev_mapping,
  1089. obj_priv->mmap_offset, obj->size, 1);
  1090. }
  1091. static void
  1092. i915_gem_free_mmap_offset(struct drm_gem_object *obj)
  1093. {
  1094. struct drm_device *dev = obj->dev;
  1095. struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
  1096. struct drm_gem_mm *mm = dev->mm_private;
  1097. struct drm_map_list *list;
  1098. list = &obj->map_list;
  1099. drm_ht_remove_item(&mm->offset_hash, &list->hash);
  1100. if (list->file_offset_node) {
  1101. drm_mm_put_block(list->file_offset_node);
  1102. list->file_offset_node = NULL;
  1103. }
  1104. if (list->map) {
  1105. kfree(list->map);
  1106. list->map = NULL;
  1107. }
  1108. obj_priv->mmap_offset = 0;
  1109. }
  1110. /**
  1111. * i915_gem_get_gtt_alignment - return required GTT alignment for an object
  1112. * @obj: object to check
  1113. *
  1114. * Return the required GTT alignment for an object, taking into account
  1115. * potential fence register mapping if needed.
  1116. */
  1117. static uint32_t
  1118. i915_gem_get_gtt_alignment(struct drm_gem_object *obj)
  1119. {
  1120. struct drm_device *dev = obj->dev;
  1121. struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
  1122. int start, i;
  1123. /*
  1124. * Minimum alignment is 4k (GTT page size), but might be greater
  1125. * if a fence register is needed for the object.
  1126. */
  1127. if (IS_I965G(dev) || obj_priv->tiling_mode == I915_TILING_NONE)
  1128. return 4096;
  1129. /*
  1130. * Previous chips need to be aligned to the size of the smallest
  1131. * fence register that can contain the object.
  1132. */
  1133. if (IS_I9XX(dev))
  1134. start = 1024*1024;
  1135. else
  1136. start = 512*1024;
  1137. for (i = start; i < obj->size; i <<= 1)
  1138. ;
  1139. return i;
  1140. }
  1141. /**
  1142. * i915_gem_mmap_gtt_ioctl - prepare an object for GTT mmap'ing
  1143. * @dev: DRM device
  1144. * @data: GTT mapping ioctl data
  1145. * @file_priv: GEM object info
  1146. *
  1147. * Simply returns the fake offset to userspace so it can mmap it.
  1148. * The mmap call will end up in drm_gem_mmap(), which will set things
  1149. * up so we can get faults in the handler above.
  1150. *
  1151. * The fault handler will take care of binding the object into the GTT
  1152. * (since it may have been evicted to make room for something), allocating
  1153. * a fence register, and mapping the appropriate aperture address into
  1154. * userspace.
  1155. */
  1156. int
  1157. i915_gem_mmap_gtt_ioctl(struct drm_device *dev, void *data,
  1158. struct drm_file *file_priv)
  1159. {
  1160. struct drm_i915_gem_mmap_gtt *args = data;
  1161. struct drm_i915_private *dev_priv = dev->dev_private;
  1162. struct drm_gem_object *obj;
  1163. struct drm_i915_gem_object *obj_priv;
  1164. int ret;
  1165. if (!(dev->driver->driver_features & DRIVER_GEM))
  1166. return -ENODEV;
  1167. obj = drm_gem_object_lookup(dev, file_priv, args->handle);
  1168. if (obj == NULL)
  1169. return -EBADF;
  1170. mutex_lock(&dev->struct_mutex);
  1171. obj_priv = to_intel_bo(obj);
  1172. if (obj_priv->madv != I915_MADV_WILLNEED) {
  1173. DRM_ERROR("Attempting to mmap a purgeable buffer\n");
  1174. drm_gem_object_unreference(obj);
  1175. mutex_unlock(&dev->struct_mutex);
  1176. return -EINVAL;
  1177. }
  1178. if (!obj_priv->mmap_offset) {
  1179. ret = i915_gem_create_mmap_offset(obj);
  1180. if (ret) {
  1181. drm_gem_object_unreference(obj);
  1182. mutex_unlock(&dev->struct_mutex);
  1183. return ret;
  1184. }
  1185. }
  1186. args->offset = obj_priv->mmap_offset;
  1187. /*
  1188. * Pull it into the GTT so that we have a page list (makes the
  1189. * initial fault faster and any subsequent flushing possible).
  1190. */
  1191. if (!obj_priv->agp_mem) {
  1192. ret = i915_gem_object_bind_to_gtt(obj, 0);
  1193. if (ret) {
  1194. drm_gem_object_unreference(obj);
  1195. mutex_unlock(&dev->struct_mutex);
  1196. return ret;
  1197. }
  1198. list_add_tail(&obj_priv->list, &dev_priv->mm.inactive_list);
  1199. }
  1200. drm_gem_object_unreference(obj);
  1201. mutex_unlock(&dev->struct_mutex);
  1202. return 0;
  1203. }
  1204. void
  1205. i915_gem_object_put_pages(struct drm_gem_object *obj)
  1206. {
  1207. struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
  1208. int page_count = obj->size / PAGE_SIZE;
  1209. int i;
  1210. BUG_ON(obj_priv->pages_refcount == 0);
  1211. BUG_ON(obj_priv->madv == __I915_MADV_PURGED);
  1212. if (--obj_priv->pages_refcount != 0)
  1213. return;
  1214. if (obj_priv->tiling_mode != I915_TILING_NONE)
  1215. i915_gem_object_save_bit_17_swizzle(obj);
  1216. if (obj_priv->madv == I915_MADV_DONTNEED)
  1217. obj_priv->dirty = 0;
  1218. for (i = 0; i < page_count; i++) {
  1219. if (obj_priv->dirty)
  1220. set_page_dirty(obj_priv->pages[i]);
  1221. if (obj_priv->madv == I915_MADV_WILLNEED)
  1222. mark_page_accessed(obj_priv->pages[i]);
  1223. page_cache_release(obj_priv->pages[i]);
  1224. }
  1225. obj_priv->dirty = 0;
  1226. drm_free_large(obj_priv->pages);
  1227. obj_priv->pages = NULL;
  1228. }
  1229. static void
  1230. i915_gem_object_move_to_active(struct drm_gem_object *obj, uint32_t seqno,
  1231. struct intel_ring_buffer *ring)
  1232. {
  1233. struct drm_device *dev = obj->dev;
  1234. drm_i915_private_t *dev_priv = dev->dev_private;
  1235. struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
  1236. BUG_ON(ring == NULL);
  1237. obj_priv->ring = ring;
  1238. /* Add a reference if we're newly entering the active list. */
  1239. if (!obj_priv->active) {
  1240. drm_gem_object_reference(obj);
  1241. obj_priv->active = 1;
  1242. }
  1243. /* Move from whatever list we were on to the tail of execution. */
  1244. spin_lock(&dev_priv->mm.active_list_lock);
  1245. list_move_tail(&obj_priv->list, &ring->active_list);
  1246. spin_unlock(&dev_priv->mm.active_list_lock);
  1247. obj_priv->last_rendering_seqno = seqno;
  1248. }
  1249. static void
  1250. i915_gem_object_move_to_flushing(struct drm_gem_object *obj)
  1251. {
  1252. struct drm_device *dev = obj->dev;
  1253. drm_i915_private_t *dev_priv = dev->dev_private;
  1254. struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
  1255. BUG_ON(!obj_priv->active);
  1256. list_move_tail(&obj_priv->list, &dev_priv->mm.flushing_list);
  1257. obj_priv->last_rendering_seqno = 0;
  1258. }
  1259. /* Immediately discard the backing storage */
  1260. static void
  1261. i915_gem_object_truncate(struct drm_gem_object *obj)
  1262. {
  1263. struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
  1264. struct inode *inode;
  1265. inode = obj->filp->f_path.dentry->d_inode;
  1266. if (inode->i_op->truncate)
  1267. inode->i_op->truncate (inode);
  1268. obj_priv->madv = __I915_MADV_PURGED;
  1269. }
  1270. static inline int
  1271. i915_gem_object_is_purgeable(struct drm_i915_gem_object *obj_priv)
  1272. {
  1273. return obj_priv->madv == I915_MADV_DONTNEED;
  1274. }
  1275. static void
  1276. i915_gem_object_move_to_inactive(struct drm_gem_object *obj)
  1277. {
  1278. struct drm_device *dev = obj->dev;
  1279. drm_i915_private_t *dev_priv = dev->dev_private;
  1280. struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
  1281. i915_verify_inactive(dev, __FILE__, __LINE__);
  1282. if (obj_priv->pin_count != 0)
  1283. list_del_init(&obj_priv->list);
  1284. else
  1285. list_move_tail(&obj_priv->list, &dev_priv->mm.inactive_list);
  1286. BUG_ON(!list_empty(&obj_priv->gpu_write_list));
  1287. obj_priv->last_rendering_seqno = 0;
  1288. obj_priv->ring = NULL;
  1289. if (obj_priv->active) {
  1290. obj_priv->active = 0;
  1291. drm_gem_object_unreference(obj);
  1292. }
  1293. i915_verify_inactive(dev, __FILE__, __LINE__);
  1294. }
  1295. static void
  1296. i915_gem_process_flushing_list(struct drm_device *dev,
  1297. uint32_t flush_domains, uint32_t seqno,
  1298. struct intel_ring_buffer *ring)
  1299. {
  1300. drm_i915_private_t *dev_priv = dev->dev_private;
  1301. struct drm_i915_gem_object *obj_priv, *next;
  1302. list_for_each_entry_safe(obj_priv, next,
  1303. &dev_priv->mm.gpu_write_list,
  1304. gpu_write_list) {
  1305. struct drm_gem_object *obj = &obj_priv->base;
  1306. if ((obj->write_domain & flush_domains) ==
  1307. obj->write_domain &&
  1308. obj_priv->ring->ring_flag == ring->ring_flag) {
  1309. uint32_t old_write_domain = obj->write_domain;
  1310. obj->write_domain = 0;
  1311. list_del_init(&obj_priv->gpu_write_list);
  1312. i915_gem_object_move_to_active(obj, seqno, ring);
  1313. /* update the fence lru list */
  1314. if (obj_priv->fence_reg != I915_FENCE_REG_NONE) {
  1315. struct drm_i915_fence_reg *reg =
  1316. &dev_priv->fence_regs[obj_priv->fence_reg];
  1317. list_move_tail(&reg->lru_list,
  1318. &dev_priv->mm.fence_list);
  1319. }
  1320. trace_i915_gem_object_change_domain(obj,
  1321. obj->read_domains,
  1322. old_write_domain);
  1323. }
  1324. }
  1325. }
  1326. uint32_t
  1327. i915_add_request(struct drm_device *dev, struct drm_file *file_priv,
  1328. uint32_t flush_domains, struct intel_ring_buffer *ring)
  1329. {
  1330. drm_i915_private_t *dev_priv = dev->dev_private;
  1331. struct drm_i915_file_private *i915_file_priv = NULL;
  1332. struct drm_i915_gem_request *request;
  1333. uint32_t seqno;
  1334. int was_empty;
  1335. if (file_priv != NULL)
  1336. i915_file_priv = file_priv->driver_priv;
  1337. request = kzalloc(sizeof(*request), GFP_KERNEL);
  1338. if (request == NULL)
  1339. return 0;
  1340. seqno = ring->add_request(dev, ring, file_priv, flush_domains);
  1341. request->seqno = seqno;
  1342. request->ring = ring;
  1343. request->emitted_jiffies = jiffies;
  1344. was_empty = list_empty(&ring->request_list);
  1345. list_add_tail(&request->list, &ring->request_list);
  1346. if (i915_file_priv) {
  1347. list_add_tail(&request->client_list,
  1348. &i915_file_priv->mm.request_list);
  1349. } else {
  1350. INIT_LIST_HEAD(&request->client_list);
  1351. }
  1352. /* Associate any objects on the flushing list matching the write
  1353. * domain we're flushing with our flush.
  1354. */
  1355. if (flush_domains != 0)
  1356. i915_gem_process_flushing_list(dev, flush_domains, seqno, ring);
  1357. if (!dev_priv->mm.suspended) {
  1358. mod_timer(&dev_priv->hangcheck_timer, jiffies + DRM_I915_HANGCHECK_PERIOD);
  1359. if (was_empty)
  1360. queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, HZ);
  1361. }
  1362. return seqno;
  1363. }
  1364. /**
  1365. * Command execution barrier
  1366. *
  1367. * Ensures that all commands in the ring are finished
  1368. * before signalling the CPU
  1369. */
  1370. static uint32_t
  1371. i915_retire_commands(struct drm_device *dev, struct intel_ring_buffer *ring)
  1372. {
  1373. uint32_t flush_domains = 0;
  1374. /* The sampler always gets flushed on i965 (sigh) */
  1375. if (IS_I965G(dev))
  1376. flush_domains |= I915_GEM_DOMAIN_SAMPLER;
  1377. ring->flush(dev, ring,
  1378. I915_GEM_DOMAIN_COMMAND, flush_domains);
  1379. return flush_domains;
  1380. }
  1381. /**
  1382. * Moves buffers associated only with the given active seqno from the active
  1383. * to inactive list, potentially freeing them.
  1384. */
  1385. static void
  1386. i915_gem_retire_request(struct drm_device *dev,
  1387. struct drm_i915_gem_request *request)
  1388. {
  1389. drm_i915_private_t *dev_priv = dev->dev_private;
  1390. trace_i915_gem_request_retire(dev, request->seqno);
  1391. /* Move any buffers on the active list that are no longer referenced
  1392. * by the ringbuffer to the flushing/inactive lists as appropriate.
  1393. */
  1394. spin_lock(&dev_priv->mm.active_list_lock);
  1395. while (!list_empty(&request->ring->active_list)) {
  1396. struct drm_gem_object *obj;
  1397. struct drm_i915_gem_object *obj_priv;
  1398. obj_priv = list_first_entry(&request->ring->active_list,
  1399. struct drm_i915_gem_object,
  1400. list);
  1401. obj = &obj_priv->base;
  1402. /* If the seqno being retired doesn't match the oldest in the
  1403. * list, then the oldest in the list must still be newer than
  1404. * this seqno.
  1405. */
  1406. if (obj_priv->last_rendering_seqno != request->seqno)
  1407. goto out;
  1408. #if WATCH_LRU
  1409. DRM_INFO("%s: retire %d moves to inactive list %p\n",
  1410. __func__, request->seqno, obj);
  1411. #endif
  1412. if (obj->write_domain != 0)
  1413. i915_gem_object_move_to_flushing(obj);
  1414. else {
  1415. /* Take a reference on the object so it won't be
  1416. * freed while the spinlock is held. The list
  1417. * protection for this spinlock is safe when breaking
  1418. * the lock like this since the next thing we do
  1419. * is just get the head of the list again.
  1420. */
  1421. drm_gem_object_reference(obj);
  1422. i915_gem_object_move_to_inactive(obj);
  1423. spin_unlock(&dev_priv->mm.active_list_lock);
  1424. drm_gem_object_unreference(obj);
  1425. spin_lock(&dev_priv->mm.active_list_lock);
  1426. }
  1427. }
  1428. out:
  1429. spin_unlock(&dev_priv->mm.active_list_lock);
  1430. }
  1431. /**
  1432. * Returns true if seq1 is later than seq2.
  1433. */
  1434. bool
  1435. i915_seqno_passed(uint32_t seq1, uint32_t seq2)
  1436. {
  1437. return (int32_t)(seq1 - seq2) >= 0;
  1438. }
  1439. uint32_t
  1440. i915_get_gem_seqno(struct drm_device *dev,
  1441. struct intel_ring_buffer *ring)
  1442. {
  1443. return ring->get_gem_seqno(dev, ring);
  1444. }
  1445. /**
  1446. * This function clears the request list as sequence numbers are passed.
  1447. */
  1448. void
  1449. i915_gem_retire_requests(struct drm_device *dev,
  1450. struct intel_ring_buffer *ring)
  1451. {
  1452. drm_i915_private_t *dev_priv = dev->dev_private;
  1453. uint32_t seqno;
  1454. if (!ring->status_page.page_addr
  1455. || list_empty(&ring->request_list))
  1456. return;
  1457. seqno = i915_get_gem_seqno(dev, ring);
  1458. while (!list_empty(&ring->request_list)) {
  1459. struct drm_i915_gem_request *request;
  1460. uint32_t retiring_seqno;
  1461. request = list_first_entry(&ring->request_list,
  1462. struct drm_i915_gem_request,
  1463. list);
  1464. retiring_seqno = request->seqno;
  1465. if (i915_seqno_passed(seqno, retiring_seqno) ||
  1466. atomic_read(&dev_priv->mm.wedged)) {
  1467. i915_gem_retire_request(dev, request);
  1468. list_del(&request->list);
  1469. list_del(&request->client_list);
  1470. kfree(request);
  1471. } else
  1472. break;
  1473. }
  1474. if (unlikely (dev_priv->trace_irq_seqno &&
  1475. i915_seqno_passed(dev_priv->trace_irq_seqno, seqno))) {
  1476. ring->user_irq_put(dev, ring);
  1477. dev_priv->trace_irq_seqno = 0;
  1478. }
  1479. }
  1480. void
  1481. i915_gem_retire_work_handler(struct work_struct *work)
  1482. {
  1483. drm_i915_private_t *dev_priv;
  1484. struct drm_device *dev;
  1485. dev_priv = container_of(work, drm_i915_private_t,
  1486. mm.retire_work.work);
  1487. dev = dev_priv->dev;
  1488. mutex_lock(&dev->struct_mutex);
  1489. i915_gem_retire_requests(dev, &dev_priv->render_ring);
  1490. if (HAS_BSD(dev))
  1491. i915_gem_retire_requests(dev, &dev_priv->bsd_ring);
  1492. if (!dev_priv->mm.suspended &&
  1493. (!list_empty(&dev_priv->render_ring.request_list) ||
  1494. (HAS_BSD(dev) &&
  1495. !list_empty(&dev_priv->bsd_ring.request_list))))
  1496. queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, HZ);
  1497. mutex_unlock(&dev->struct_mutex);
  1498. }
  1499. int
  1500. i915_do_wait_request(struct drm_device *dev, uint32_t seqno,
  1501. int interruptible, struct intel_ring_buffer *ring)
  1502. {
  1503. drm_i915_private_t *dev_priv = dev->dev_private;
  1504. u32 ier;
  1505. int ret = 0;
  1506. BUG_ON(seqno == 0);
  1507. if (atomic_read(&dev_priv->mm.wedged))
  1508. return -EIO;
  1509. if (!i915_seqno_passed(ring->get_gem_seqno(dev, ring), seqno)) {
  1510. if (HAS_PCH_SPLIT(dev))
  1511. ier = I915_READ(DEIER) | I915_READ(GTIER);
  1512. else
  1513. ier = I915_READ(IER);
  1514. if (!ier) {
  1515. DRM_ERROR("something (likely vbetool) disabled "
  1516. "interrupts, re-enabling\n");
  1517. i915_driver_irq_preinstall(dev);
  1518. i915_driver_irq_postinstall(dev);
  1519. }
  1520. trace_i915_gem_request_wait_begin(dev, seqno);
  1521. ring->waiting_gem_seqno = seqno;
  1522. ring->user_irq_get(dev, ring);
  1523. if (interruptible)
  1524. ret = wait_event_interruptible(ring->irq_queue,
  1525. i915_seqno_passed(
  1526. ring->get_gem_seqno(dev, ring), seqno)
  1527. || atomic_read(&dev_priv->mm.wedged));
  1528. else
  1529. wait_event(ring->irq_queue,
  1530. i915_seqno_passed(
  1531. ring->get_gem_seqno(dev, ring), seqno)
  1532. || atomic_read(&dev_priv->mm.wedged));
  1533. ring->user_irq_put(dev, ring);
  1534. ring->waiting_gem_seqno = 0;
  1535. trace_i915_gem_request_wait_end(dev, seqno);
  1536. }
  1537. if (atomic_read(&dev_priv->mm.wedged))
  1538. ret = -EIO;
  1539. if (ret && ret != -ERESTARTSYS)
  1540. DRM_ERROR("%s returns %d (awaiting %d at %d)\n",
  1541. __func__, ret, seqno, ring->get_gem_seqno(dev, ring));
  1542. /* Directly dispatch request retiring. While we have the work queue
  1543. * to handle this, the waiter on a request often wants an associated
  1544. * buffer to have made it to the inactive list, and we would need
  1545. * a separate wait queue to handle that.
  1546. */
  1547. if (ret == 0)
  1548. i915_gem_retire_requests(dev, ring);
  1549. return ret;
  1550. }
  1551. /**
  1552. * Waits for a sequence number to be signaled, and cleans up the
  1553. * request and object lists appropriately for that event.
  1554. */
  1555. static int
  1556. i915_wait_request(struct drm_device *dev, uint32_t seqno,
  1557. struct intel_ring_buffer *ring)
  1558. {
  1559. return i915_do_wait_request(dev, seqno, 1, ring);
  1560. }
  1561. static void
  1562. i915_gem_flush(struct drm_device *dev,
  1563. uint32_t invalidate_domains,
  1564. uint32_t flush_domains)
  1565. {
  1566. drm_i915_private_t *dev_priv = dev->dev_private;
  1567. if (flush_domains & I915_GEM_DOMAIN_CPU)
  1568. drm_agp_chipset_flush(dev);
  1569. dev_priv->render_ring.flush(dev, &dev_priv->render_ring,
  1570. invalidate_domains,
  1571. flush_domains);
  1572. if (HAS_BSD(dev))
  1573. dev_priv->bsd_ring.flush(dev, &dev_priv->bsd_ring,
  1574. invalidate_domains,
  1575. flush_domains);
  1576. }
  1577. static void
  1578. i915_gem_flush_ring(struct drm_device *dev,
  1579. uint32_t invalidate_domains,
  1580. uint32_t flush_domains,
  1581. struct intel_ring_buffer *ring)
  1582. {
  1583. if (flush_domains & I915_GEM_DOMAIN_CPU)
  1584. drm_agp_chipset_flush(dev);
  1585. ring->flush(dev, ring,
  1586. invalidate_domains,
  1587. flush_domains);
  1588. }
  1589. /**
  1590. * Ensures that all rendering to the object has completed and the object is
  1591. * safe to unbind from the GTT or access from the CPU.
  1592. */
  1593. static int
  1594. i915_gem_object_wait_rendering(struct drm_gem_object *obj)
  1595. {
  1596. struct drm_device *dev = obj->dev;
  1597. struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
  1598. int ret;
  1599. /* This function only exists to support waiting for existing rendering,
  1600. * not for emitting required flushes.
  1601. */
  1602. BUG_ON((obj->write_domain & I915_GEM_GPU_DOMAINS) != 0);
  1603. /* If there is rendering queued on the buffer being evicted, wait for
  1604. * it.
  1605. */
  1606. if (obj_priv->active) {
  1607. #if WATCH_BUF
  1608. DRM_INFO("%s: object %p wait for seqno %08x\n",
  1609. __func__, obj, obj_priv->last_rendering_seqno);
  1610. #endif
  1611. ret = i915_wait_request(dev,
  1612. obj_priv->last_rendering_seqno, obj_priv->ring);
  1613. if (ret != 0)
  1614. return ret;
  1615. }
  1616. return 0;
  1617. }
  1618. /**
  1619. * Unbinds an object from the GTT aperture.
  1620. */
  1621. int
  1622. i915_gem_object_unbind(struct drm_gem_object *obj)
  1623. {
  1624. struct drm_device *dev = obj->dev;
  1625. drm_i915_private_t *dev_priv = dev->dev_private;
  1626. struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
  1627. int ret = 0;
  1628. #if WATCH_BUF
  1629. DRM_INFO("%s:%d %p\n", __func__, __LINE__, obj);
  1630. DRM_INFO("gtt_space %p\n", obj_priv->gtt_space);
  1631. #endif
  1632. if (obj_priv->gtt_space == NULL)
  1633. return 0;
  1634. if (obj_priv->pin_count != 0) {
  1635. DRM_ERROR("Attempting to unbind pinned buffer\n");
  1636. return -EINVAL;
  1637. }
  1638. /* blow away mappings if mapped through GTT */
  1639. i915_gem_release_mmap(obj);
  1640. /* Move the object to the CPU domain to ensure that
  1641. * any possible CPU writes while it's not in the GTT
  1642. * are flushed when we go to remap it. This will
  1643. * also ensure that all pending GPU writes are finished
  1644. * before we unbind.
  1645. */
  1646. ret = i915_gem_object_set_to_cpu_domain(obj, 1);
  1647. if (ret) {
  1648. if (ret != -ERESTARTSYS)
  1649. DRM_ERROR("set_domain failed: %d\n", ret);
  1650. return ret;
  1651. }
  1652. BUG_ON(obj_priv->active);
  1653. /* release the fence reg _after_ flushing */
  1654. if (obj_priv->fence_reg != I915_FENCE_REG_NONE)
  1655. i915_gem_clear_fence_reg(obj);
  1656. if (obj_priv->agp_mem != NULL) {
  1657. drm_unbind_agp(obj_priv->agp_mem);
  1658. drm_free_agp(obj_priv->agp_mem, obj->size / PAGE_SIZE);
  1659. obj_priv->agp_mem = NULL;
  1660. }
  1661. i915_gem_object_put_pages(obj);
  1662. BUG_ON(obj_priv->pages_refcount);
  1663. if (obj_priv->gtt_space) {
  1664. atomic_dec(&dev->gtt_count);
  1665. atomic_sub(obj->size, &dev->gtt_memory);
  1666. drm_mm_put_block(obj_priv->gtt_space);
  1667. obj_priv->gtt_space = NULL;
  1668. }
  1669. /* Remove ourselves from the LRU list if present. */
  1670. spin_lock(&dev_priv->mm.active_list_lock);
  1671. if (!list_empty(&obj_priv->list))
  1672. list_del_init(&obj_priv->list);
  1673. spin_unlock(&dev_priv->mm.active_list_lock);
  1674. if (i915_gem_object_is_purgeable(obj_priv))
  1675. i915_gem_object_truncate(obj);
  1676. trace_i915_gem_object_unbind(obj);
  1677. return 0;
  1678. }
  1679. static struct drm_gem_object *
  1680. i915_gem_find_inactive_object(struct drm_device *dev, int min_size)
  1681. {
  1682. drm_i915_private_t *dev_priv = dev->dev_private;
  1683. struct drm_i915_gem_object *obj_priv;
  1684. struct drm_gem_object *best = NULL;
  1685. struct drm_gem_object *first = NULL;
  1686. /* Try to find the smallest clean object */
  1687. list_for_each_entry(obj_priv, &dev_priv->mm.inactive_list, list) {
  1688. struct drm_gem_object *obj = &obj_priv->base;
  1689. if (obj->size >= min_size) {
  1690. if ((!obj_priv->dirty ||
  1691. i915_gem_object_is_purgeable(obj_priv)) &&
  1692. (!best || obj->size < best->size)) {
  1693. best = obj;
  1694. if (best->size == min_size)
  1695. return best;
  1696. }
  1697. if (!first)
  1698. first = obj;
  1699. }
  1700. }
  1701. return best ? best : first;
  1702. }
  1703. static int
  1704. i915_gpu_idle(struct drm_device *dev)
  1705. {
  1706. drm_i915_private_t *dev_priv = dev->dev_private;
  1707. bool lists_empty;
  1708. uint32_t seqno1, seqno2;
  1709. int ret;
  1710. spin_lock(&dev_priv->mm.active_list_lock);
  1711. lists_empty = (list_empty(&dev_priv->mm.flushing_list) &&
  1712. list_empty(&dev_priv->render_ring.active_list) &&
  1713. (!HAS_BSD(dev) ||
  1714. list_empty(&dev_priv->bsd_ring.active_list)));
  1715. spin_unlock(&dev_priv->mm.active_list_lock);
  1716. if (lists_empty)
  1717. return 0;
  1718. /* Flush everything onto the inactive list. */
  1719. i915_gem_flush(dev, I915_GEM_GPU_DOMAINS, I915_GEM_GPU_DOMAINS);
  1720. seqno1 = i915_add_request(dev, NULL, I915_GEM_GPU_DOMAINS,
  1721. &dev_priv->render_ring);
  1722. if (seqno1 == 0)
  1723. return -ENOMEM;
  1724. ret = i915_wait_request(dev, seqno1, &dev_priv->render_ring);
  1725. if (HAS_BSD(dev)) {
  1726. seqno2 = i915_add_request(dev, NULL, I915_GEM_GPU_DOMAINS,
  1727. &dev_priv->bsd_ring);
  1728. if (seqno2 == 0)
  1729. return -ENOMEM;
  1730. ret = i915_wait_request(dev, seqno2, &dev_priv->bsd_ring);
  1731. if (ret)
  1732. return ret;
  1733. }
  1734. return ret;
  1735. }
  1736. static int
  1737. i915_gem_evict_everything(struct drm_device *dev)
  1738. {
  1739. drm_i915_private_t *dev_priv = dev->dev_private;
  1740. int ret;
  1741. bool lists_empty;
  1742. spin_lock(&dev_priv->mm.active_list_lock);
  1743. lists_empty = (list_empty(&dev_priv->mm.inactive_list) &&
  1744. list_empty(&dev_priv->mm.flushing_list) &&
  1745. list_empty(&dev_priv->render_ring.active_list) &&
  1746. (!HAS_BSD(dev)
  1747. || list_empty(&dev_priv->bsd_ring.active_list)));
  1748. spin_unlock(&dev_priv->mm.active_list_lock);
  1749. if (lists_empty)
  1750. return -ENOSPC;
  1751. /* Flush everything (on to the inactive lists) and evict */
  1752. ret = i915_gpu_idle(dev);
  1753. if (ret)
  1754. return ret;
  1755. BUG_ON(!list_empty(&dev_priv->mm.flushing_list));
  1756. ret = i915_gem_evict_from_inactive_list(dev);
  1757. if (ret)
  1758. return ret;
  1759. spin_lock(&dev_priv->mm.active_list_lock);
  1760. lists_empty = (list_empty(&dev_priv->mm.inactive_list) &&
  1761. list_empty(&dev_priv->mm.flushing_list) &&
  1762. list_empty(&dev_priv->render_ring.active_list) &&
  1763. (!HAS_BSD(dev)
  1764. || list_empty(&dev_priv->bsd_ring.active_list)));
  1765. spin_unlock(&dev_priv->mm.active_list_lock);
  1766. BUG_ON(!lists_empty);
  1767. return 0;
  1768. }
  1769. static int
  1770. i915_gem_evict_something(struct drm_device *dev, int min_size)
  1771. {
  1772. drm_i915_private_t *dev_priv = dev->dev_private;
  1773. struct drm_gem_object *obj;
  1774. int ret;
  1775. struct intel_ring_buffer *render_ring = &dev_priv->render_ring;
  1776. struct intel_ring_buffer *bsd_ring = &dev_priv->bsd_ring;
  1777. for (;;) {
  1778. i915_gem_retire_requests(dev, render_ring);
  1779. if (HAS_BSD(dev))
  1780. i915_gem_retire_requests(dev, bsd_ring);
  1781. /* If there's an inactive buffer available now, grab it
  1782. * and be done.
  1783. */
  1784. obj = i915_gem_find_inactive_object(dev, min_size);
  1785. if (obj) {
  1786. struct drm_i915_gem_object *obj_priv;
  1787. #if WATCH_LRU
  1788. DRM_INFO("%s: evicting %p\n", __func__, obj);
  1789. #endif
  1790. obj_priv = to_intel_bo(obj);
  1791. BUG_ON(obj_priv->pin_count != 0);
  1792. BUG_ON(obj_priv->active);
  1793. /* Wait on the rendering and unbind the buffer. */
  1794. return i915_gem_object_unbind(obj);
  1795. }
  1796. /* If we didn't get anything, but the ring is still processing
  1797. * things, wait for the next to finish and hopefully leave us
  1798. * a buffer to evict.
  1799. */
  1800. if (!list_empty(&render_ring->request_list)) {
  1801. struct drm_i915_gem_request *request;
  1802. request = list_first_entry(&render_ring->request_list,
  1803. struct drm_i915_gem_request,
  1804. list);
  1805. ret = i915_wait_request(dev,
  1806. request->seqno, request->ring);
  1807. if (ret)
  1808. return ret;
  1809. continue;
  1810. }
  1811. if (HAS_BSD(dev) && !list_empty(&bsd_ring->request_list)) {
  1812. struct drm_i915_gem_request *request;
  1813. request = list_first_entry(&bsd_ring->request_list,
  1814. struct drm_i915_gem_request,
  1815. list);
  1816. ret = i915_wait_request(dev,
  1817. request->seqno, request->ring);
  1818. if (ret)
  1819. return ret;
  1820. continue;
  1821. }
  1822. /* If we didn't have anything on the request list but there
  1823. * are buffers awaiting a flush, emit one and try again.
  1824. * When we wait on it, those buffers waiting for that flush
  1825. * will get moved to inactive.
  1826. */
  1827. if (!list_empty(&dev_priv->mm.flushing_list)) {
  1828. struct drm_i915_gem_object *obj_priv;
  1829. /* Find an object that we can immediately reuse */
  1830. list_for_each_entry(obj_priv, &dev_priv->mm.flushing_list, list) {
  1831. obj = &obj_priv->base;
  1832. if (obj->size >= min_size)
  1833. break;
  1834. obj = NULL;
  1835. }
  1836. if (obj != NULL) {
  1837. uint32_t seqno;
  1838. i915_gem_flush_ring(dev,
  1839. obj->write_domain,
  1840. obj->write_domain,
  1841. obj_priv->ring);
  1842. seqno = i915_add_request(dev, NULL,
  1843. obj->write_domain,
  1844. obj_priv->ring);
  1845. if (seqno == 0)
  1846. return -ENOMEM;
  1847. continue;
  1848. }
  1849. }
  1850. /* If we didn't do any of the above, there's no single buffer
  1851. * large enough to swap out for the new one, so just evict
  1852. * everything and start again. (This should be rare.)
  1853. */
  1854. if (!list_empty (&dev_priv->mm.inactive_list))
  1855. return i915_gem_evict_from_inactive_list(dev);
  1856. else
  1857. return i915_gem_evict_everything(dev);
  1858. }
  1859. }
  1860. int
  1861. i915_gem_object_get_pages(struct drm_gem_object *obj,
  1862. gfp_t gfpmask)
  1863. {
  1864. struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
  1865. int page_count, i;
  1866. struct address_space *mapping;
  1867. struct inode *inode;
  1868. struct page *page;
  1869. BUG_ON(obj_priv->pages_refcount
  1870. == DRM_I915_GEM_OBJECT_MAX_PAGES_REFCOUNT);
  1871. if (obj_priv->pages_refcount++ != 0)
  1872. return 0;
  1873. /* Get the list of pages out of our struct file. They'll be pinned
  1874. * at this point until we release them.
  1875. */
  1876. page_count = obj->size / PAGE_SIZE;
  1877. BUG_ON(obj_priv->pages != NULL);
  1878. obj_priv->pages = drm_calloc_large(page_count, sizeof(struct page *));
  1879. if (obj_priv->pages == NULL) {
  1880. obj_priv->pages_refcount--;
  1881. return -ENOMEM;
  1882. }
  1883. inode = obj->filp->f_path.dentry->d_inode;
  1884. mapping = inode->i_mapping;
  1885. for (i = 0; i < page_count; i++) {
  1886. page = read_cache_page_gfp(mapping, i,
  1887. GFP_HIGHUSER |
  1888. __GFP_COLD |
  1889. __GFP_RECLAIMABLE |
  1890. gfpmask);
  1891. if (IS_ERR(page))
  1892. goto err_pages;
  1893. obj_priv->pages[i] = page;
  1894. }
  1895. if (obj_priv->tiling_mode != I915_TILING_NONE)
  1896. i915_gem_object_do_bit_17_swizzle(obj);
  1897. return 0;
  1898. err_pages:
  1899. while (i--)
  1900. page_cache_release(obj_priv->pages[i]);
  1901. drm_free_large(obj_priv->pages);
  1902. obj_priv->pages = NULL;
  1903. obj_priv->pages_refcount--;
  1904. return PTR_ERR(page);
  1905. }
  1906. static void sandybridge_write_fence_reg(struct drm_i915_fence_reg *reg)
  1907. {
  1908. struct drm_gem_object *obj = reg->obj;
  1909. struct drm_device *dev = obj->dev;
  1910. drm_i915_private_t *dev_priv = dev->dev_private;
  1911. struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
  1912. int regnum = obj_priv->fence_reg;
  1913. uint64_t val;
  1914. val = (uint64_t)((obj_priv->gtt_offset + obj->size - 4096) &
  1915. 0xfffff000) << 32;
  1916. val |= obj_priv->gtt_offset & 0xfffff000;
  1917. val |= (uint64_t)((obj_priv->stride / 128) - 1) <<
  1918. SANDYBRIDGE_FENCE_PITCH_SHIFT;
  1919. if (obj_priv->tiling_mode == I915_TILING_Y)
  1920. val |= 1 << I965_FENCE_TILING_Y_SHIFT;
  1921. val |= I965_FENCE_REG_VALID;
  1922. I915_WRITE64(FENCE_REG_SANDYBRIDGE_0 + (regnum * 8), val);
  1923. }
  1924. static void i965_write_fence_reg(struct drm_i915_fence_reg *reg)
  1925. {
  1926. struct drm_gem_object *obj = reg->obj;
  1927. struct drm_device *dev = obj->dev;
  1928. drm_i915_private_t *dev_priv = dev->dev_private;
  1929. struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
  1930. int regnum = obj_priv->fence_reg;
  1931. uint64_t val;
  1932. val = (uint64_t)((obj_priv->gtt_offset + obj->size - 4096) &
  1933. 0xfffff000) << 32;
  1934. val |= obj_priv->gtt_offset & 0xfffff000;
  1935. val |= ((obj_priv->stride / 128) - 1) << I965_FENCE_PITCH_SHIFT;
  1936. if (obj_priv->tiling_mode == I915_TILING_Y)
  1937. val |= 1 << I965_FENCE_TILING_Y_SHIFT;
  1938. val |= I965_FENCE_REG_VALID;
  1939. I915_WRITE64(FENCE_REG_965_0 + (regnum * 8), val);
  1940. }
  1941. static void i915_write_fence_reg(struct drm_i915_fence_reg *reg)
  1942. {
  1943. struct drm_gem_object *obj = reg->obj;
  1944. struct drm_device *dev = obj->dev;
  1945. drm_i915_private_t *dev_priv = dev->dev_private;
  1946. struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
  1947. int regnum = obj_priv->fence_reg;
  1948. int tile_width;
  1949. uint32_t fence_reg, val;
  1950. uint32_t pitch_val;
  1951. if ((obj_priv->gtt_offset & ~I915_FENCE_START_MASK) ||
  1952. (obj_priv->gtt_offset & (obj->size - 1))) {
  1953. WARN(1, "%s: object 0x%08x not 1M or size (0x%zx) aligned\n",
  1954. __func__, obj_priv->gtt_offset, obj->size);
  1955. return;
  1956. }
  1957. if (obj_priv->tiling_mode == I915_TILING_Y &&
  1958. HAS_128_BYTE_Y_TILING(dev))
  1959. tile_width = 128;
  1960. else
  1961. tile_width = 512;
  1962. /* Note: pitch better be a power of two tile widths */
  1963. pitch_val = obj_priv->stride / tile_width;
  1964. pitch_val = ffs(pitch_val) - 1;
  1965. if (obj_priv->tiling_mode == I915_TILING_Y &&
  1966. HAS_128_BYTE_Y_TILING(dev))
  1967. WARN_ON(pitch_val > I830_FENCE_MAX_PITCH_VAL);
  1968. else
  1969. WARN_ON(pitch_val > I915_FENCE_MAX_PITCH_VAL);
  1970. val = obj_priv->gtt_offset;
  1971. if (obj_priv->tiling_mode == I915_TILING_Y)
  1972. val |= 1 << I830_FENCE_TILING_Y_SHIFT;
  1973. val |= I915_FENCE_SIZE_BITS(obj->size);
  1974. val |= pitch_val << I830_FENCE_PITCH_SHIFT;
  1975. val |= I830_FENCE_REG_VALID;
  1976. if (regnum < 8)
  1977. fence_reg = FENCE_REG_830_0 + (regnum * 4);
  1978. else
  1979. fence_reg = FENCE_REG_945_8 + ((regnum - 8) * 4);
  1980. I915_WRITE(fence_reg, val);
  1981. }
  1982. static void i830_write_fence_reg(struct drm_i915_fence_reg *reg)
  1983. {
  1984. struct drm_gem_object *obj = reg->obj;
  1985. struct drm_device *dev = obj->dev;
  1986. drm_i915_private_t *dev_priv = dev->dev_private;
  1987. struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
  1988. int regnum = obj_priv->fence_reg;
  1989. uint32_t val;
  1990. uint32_t pitch_val;
  1991. uint32_t fence_size_bits;
  1992. if ((obj_priv->gtt_offset & ~I830_FENCE_START_MASK) ||
  1993. (obj_priv->gtt_offset & (obj->size - 1))) {
  1994. WARN(1, "%s: object 0x%08x not 512K or size aligned\n",
  1995. __func__, obj_priv->gtt_offset);
  1996. return;
  1997. }
  1998. pitch_val = obj_priv->stride / 128;
  1999. pitch_val = ffs(pitch_val) - 1;
  2000. WARN_ON(pitch_val > I830_FENCE_MAX_PITCH_VAL);
  2001. val = obj_priv->gtt_offset;
  2002. if (obj_priv->tiling_mode == I915_TILING_Y)
  2003. val |= 1 << I830_FENCE_TILING_Y_SHIFT;
  2004. fence_size_bits = I830_FENCE_SIZE_BITS(obj->size);
  2005. WARN_ON(fence_size_bits & ~0x00000f00);
  2006. val |= fence_size_bits;
  2007. val |= pitch_val << I830_FENCE_PITCH_SHIFT;
  2008. val |= I830_FENCE_REG_VALID;
  2009. I915_WRITE(FENCE_REG_830_0 + (regnum * 4), val);
  2010. }
  2011. static int i915_find_fence_reg(struct drm_device *dev)
  2012. {
  2013. struct drm_i915_fence_reg *reg = NULL;
  2014. struct drm_i915_gem_object *obj_priv = NULL;
  2015. struct drm_i915_private *dev_priv = dev->dev_private;
  2016. struct drm_gem_object *obj = NULL;
  2017. int i, avail, ret;
  2018. /* First try to find a free reg */
  2019. avail = 0;
  2020. for (i = dev_priv->fence_reg_start; i < dev_priv->num_fence_regs; i++) {
  2021. reg = &dev_priv->fence_regs[i];
  2022. if (!reg->obj)
  2023. return i;
  2024. obj_priv = to_intel_bo(reg->obj);
  2025. if (!obj_priv->pin_count)
  2026. avail++;
  2027. }
  2028. if (avail == 0)
  2029. return -ENOSPC;
  2030. /* None available, try to steal one or wait for a user to finish */
  2031. i = I915_FENCE_REG_NONE;
  2032. list_for_each_entry(reg, &dev_priv->mm.fence_list,
  2033. lru_list) {
  2034. obj = reg->obj;
  2035. obj_priv = to_intel_bo(obj);
  2036. if (obj_priv->pin_count)
  2037. continue;
  2038. /* found one! */
  2039. i = obj_priv->fence_reg;
  2040. break;
  2041. }
  2042. BUG_ON(i == I915_FENCE_REG_NONE);
  2043. /* We only have a reference on obj from the active list. put_fence_reg
  2044. * might drop that one, causing a use-after-free in it. So hold a
  2045. * private reference to obj like the other callers of put_fence_reg
  2046. * (set_tiling ioctl) do. */
  2047. drm_gem_object_reference(obj);
  2048. ret = i915_gem_object_put_fence_reg(obj);
  2049. drm_gem_object_unreference(obj);
  2050. if (ret != 0)
  2051. return ret;
  2052. return i;
  2053. }
  2054. /**
  2055. * i915_gem_object_get_fence_reg - set up a fence reg for an object
  2056. * @obj: object to map through a fence reg
  2057. *
  2058. * When mapping objects through the GTT, userspace wants to be able to write
  2059. * to them without having to worry about swizzling if the object is tiled.
  2060. *
  2061. * This function walks the fence regs looking for a free one for @obj,
  2062. * stealing one if it can't find any.
  2063. *
  2064. * It then sets up the reg based on the object's properties: address, pitch
  2065. * and tiling format.
  2066. */
  2067. int
  2068. i915_gem_object_get_fence_reg(struct drm_gem_object *obj)
  2069. {
  2070. struct drm_device *dev = obj->dev;
  2071. struct drm_i915_private *dev_priv = dev->dev_private;
  2072. struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
  2073. struct drm_i915_fence_reg *reg = NULL;
  2074. int ret;
  2075. /* Just update our place in the LRU if our fence is getting used. */
  2076. if (obj_priv->fence_reg != I915_FENCE_REG_NONE) {
  2077. reg = &dev_priv->fence_regs[obj_priv->fence_reg];
  2078. list_move_tail(&reg->lru_list, &dev_priv->mm.fence_list);
  2079. return 0;
  2080. }
  2081. switch (obj_priv->tiling_mode) {
  2082. case I915_TILING_NONE:
  2083. WARN(1, "allocating a fence for non-tiled object?\n");
  2084. break;
  2085. case I915_TILING_X:
  2086. if (!obj_priv->stride)
  2087. return -EINVAL;
  2088. WARN((obj_priv->stride & (512 - 1)),
  2089. "object 0x%08x is X tiled but has non-512B pitch\n",
  2090. obj_priv->gtt_offset);
  2091. break;
  2092. case I915_TILING_Y:
  2093. if (!obj_priv->stride)
  2094. return -EINVAL;
  2095. WARN((obj_priv->stride & (128 - 1)),
  2096. "object 0x%08x is Y tiled but has non-128B pitch\n",
  2097. obj_priv->gtt_offset);
  2098. break;
  2099. }
  2100. ret = i915_find_fence_reg(dev);
  2101. if (ret < 0)
  2102. return ret;
  2103. obj_priv->fence_reg = ret;
  2104. reg = &dev_priv->fence_regs[obj_priv->fence_reg];
  2105. list_add_tail(&reg->lru_list, &dev_priv->mm.fence_list);
  2106. reg->obj = obj;
  2107. if (IS_GEN6(dev))
  2108. sandybridge_write_fence_reg(reg);
  2109. else if (IS_I965G(dev))
  2110. i965_write_fence_reg(reg);
  2111. else if (IS_I9XX(dev))
  2112. i915_write_fence_reg(reg);
  2113. else
  2114. i830_write_fence_reg(reg);
  2115. trace_i915_gem_object_get_fence(obj, obj_priv->fence_reg,
  2116. obj_priv->tiling_mode);
  2117. return 0;
  2118. }
  2119. /**
  2120. * i915_gem_clear_fence_reg - clear out fence register info
  2121. * @obj: object to clear
  2122. *
  2123. * Zeroes out the fence register itself and clears out the associated
  2124. * data structures in dev_priv and obj_priv.
  2125. */
  2126. static void
  2127. i915_gem_clear_fence_reg(struct drm_gem_object *obj)
  2128. {
  2129. struct drm_device *dev = obj->dev;
  2130. drm_i915_private_t *dev_priv = dev->dev_private;
  2131. struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
  2132. struct drm_i915_fence_reg *reg =
  2133. &dev_priv->fence_regs[obj_priv->fence_reg];
  2134. if (IS_GEN6(dev)) {
  2135. I915_WRITE64(FENCE_REG_SANDYBRIDGE_0 +
  2136. (obj_priv->fence_reg * 8), 0);
  2137. } else if (IS_I965G(dev)) {
  2138. I915_WRITE64(FENCE_REG_965_0 + (obj_priv->fence_reg * 8), 0);
  2139. } else {
  2140. uint32_t fence_reg;
  2141. if (obj_priv->fence_reg < 8)
  2142. fence_reg = FENCE_REG_830_0 + obj_priv->fence_reg * 4;
  2143. else
  2144. fence_reg = FENCE_REG_945_8 + (obj_priv->fence_reg -
  2145. 8) * 4;
  2146. I915_WRITE(fence_reg, 0);
  2147. }
  2148. reg->obj = NULL;
  2149. obj_priv->fence_reg = I915_FENCE_REG_NONE;
  2150. list_del_init(&reg->lru_list);
  2151. }
  2152. /**
  2153. * i915_gem_object_put_fence_reg - waits on outstanding fenced access
  2154. * to the buffer to finish, and then resets the fence register.
  2155. * @obj: tiled object holding a fence register.
  2156. *
  2157. * Zeroes out the fence register itself and clears out the associated
  2158. * data structures in dev_priv and obj_priv.
  2159. */
  2160. int
  2161. i915_gem_object_put_fence_reg(struct drm_gem_object *obj)
  2162. {
  2163. struct drm_device *dev = obj->dev;
  2164. struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
  2165. if (obj_priv->fence_reg == I915_FENCE_REG_NONE)
  2166. return 0;
  2167. /* If we've changed tiling, GTT-mappings of the object
  2168. * need to re-fault to ensure that the correct fence register
  2169. * setup is in place.
  2170. */
  2171. i915_gem_release_mmap(obj);
  2172. /* On the i915, GPU access to tiled buffers is via a fence,
  2173. * therefore we must wait for any outstanding access to complete
  2174. * before clearing the fence.
  2175. */
  2176. if (!IS_I965G(dev)) {
  2177. int ret;
  2178. i915_gem_object_flush_gpu_write_domain(obj);
  2179. ret = i915_gem_object_wait_rendering(obj);
  2180. if (ret != 0)
  2181. return ret;
  2182. }
  2183. i915_gem_object_flush_gtt_write_domain(obj);
  2184. i915_gem_clear_fence_reg (obj);
  2185. return 0;
  2186. }
  2187. /**
  2188. * Finds free space in the GTT aperture and binds the object there.
  2189. */
  2190. static int
  2191. i915_gem_object_bind_to_gtt(struct drm_gem_object *obj, unsigned alignment)
  2192. {
  2193. struct drm_device *dev = obj->dev;
  2194. drm_i915_private_t *dev_priv = dev->dev_private;
  2195. struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
  2196. struct drm_mm_node *free_space;
  2197. gfp_t gfpmask = __GFP_NORETRY | __GFP_NOWARN;
  2198. int ret;
  2199. if (obj_priv->madv != I915_MADV_WILLNEED) {
  2200. DRM_ERROR("Attempting to bind a purgeable object\n");
  2201. return -EINVAL;
  2202. }
  2203. if (alignment == 0)
  2204. alignment = i915_gem_get_gtt_alignment(obj);
  2205. if (alignment & (i915_gem_get_gtt_alignment(obj) - 1)) {
  2206. DRM_ERROR("Invalid object alignment requested %u\n", alignment);
  2207. return -EINVAL;
  2208. }
  2209. /* If the object is bigger than the entire aperture, reject it early
  2210. * before evicting everything in a vain attempt to find space.
  2211. */
  2212. if (obj->size > dev->gtt_total) {
  2213. DRM_ERROR("Attempting to bind an object larger than the aperture\n");
  2214. return -E2BIG;
  2215. }
  2216. search_free:
  2217. free_space = drm_mm_search_free(&dev_priv->mm.gtt_space,
  2218. obj->size, alignment, 0);
  2219. if (free_space != NULL) {
  2220. obj_priv->gtt_space = drm_mm_get_block(free_space, obj->size,
  2221. alignment);
  2222. if (obj_priv->gtt_space != NULL) {
  2223. obj_priv->gtt_space->private = obj;
  2224. obj_priv->gtt_offset = obj_priv->gtt_space->start;
  2225. }
  2226. }
  2227. if (obj_priv->gtt_space == NULL) {
  2228. /* If the gtt is empty and we're still having trouble
  2229. * fitting our object in, we're out of memory.
  2230. */
  2231. #if WATCH_LRU
  2232. DRM_INFO("%s: GTT full, evicting something\n", __func__);
  2233. #endif
  2234. ret = i915_gem_evict_something(dev, obj->size);
  2235. if (ret)
  2236. return ret;
  2237. goto search_free;
  2238. }
  2239. #if WATCH_BUF
  2240. DRM_INFO("Binding object of size %zd at 0x%08x\n",
  2241. obj->size, obj_priv->gtt_offset);
  2242. #endif
  2243. ret = i915_gem_object_get_pages(obj, gfpmask);
  2244. if (ret) {
  2245. drm_mm_put_block(obj_priv->gtt_space);
  2246. obj_priv->gtt_space = NULL;
  2247. if (ret == -ENOMEM) {
  2248. /* first try to clear up some space from the GTT */
  2249. ret = i915_gem_evict_something(dev, obj->size);
  2250. if (ret) {
  2251. /* now try to shrink everyone else */
  2252. if (gfpmask) {
  2253. gfpmask = 0;
  2254. goto search_free;
  2255. }
  2256. return ret;
  2257. }
  2258. goto search_free;
  2259. }
  2260. return ret;
  2261. }
  2262. /* Create an AGP memory structure pointing at our pages, and bind it
  2263. * into the GTT.
  2264. */
  2265. obj_priv->agp_mem = drm_agp_bind_pages(dev,
  2266. obj_priv->pages,
  2267. obj->size >> PAGE_SHIFT,
  2268. obj_priv->gtt_offset,
  2269. obj_priv->agp_type);
  2270. if (obj_priv->agp_mem == NULL) {
  2271. i915_gem_object_put_pages(obj);
  2272. drm_mm_put_block(obj_priv->gtt_space);
  2273. obj_priv->gtt_space = NULL;
  2274. ret = i915_gem_evict_something(dev, obj->size);
  2275. if (ret)
  2276. return ret;
  2277. goto search_free;
  2278. }
  2279. atomic_inc(&dev->gtt_count);
  2280. atomic_add(obj->size, &dev->gtt_memory);
  2281. /* Assert that the object is not currently in any GPU domain. As it
  2282. * wasn't in the GTT, there shouldn't be any way it could have been in
  2283. * a GPU cache
  2284. */
  2285. BUG_ON(obj->read_domains & I915_GEM_GPU_DOMAINS);
  2286. BUG_ON(obj->write_domain & I915_GEM_GPU_DOMAINS);
  2287. trace_i915_gem_object_bind(obj, obj_priv->gtt_offset);
  2288. return 0;
  2289. }
  2290. void
  2291. i915_gem_clflush_object(struct drm_gem_object *obj)
  2292. {
  2293. struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
  2294. /* If we don't have a page list set up, then we're not pinned
  2295. * to GPU, and we can ignore the cache flush because it'll happen
  2296. * again at bind time.
  2297. */
  2298. if (obj_priv->pages == NULL)
  2299. return;
  2300. trace_i915_gem_object_clflush(obj);
  2301. drm_clflush_pages(obj_priv->pages, obj->size / PAGE_SIZE);
  2302. }
  2303. /** Flushes any GPU write domain for the object if it's dirty. */
  2304. static void
  2305. i915_gem_object_flush_gpu_write_domain(struct drm_gem_object *obj)
  2306. {
  2307. struct drm_device *dev = obj->dev;
  2308. uint32_t old_write_domain;
  2309. struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
  2310. if ((obj->write_domain & I915_GEM_GPU_DOMAINS) == 0)
  2311. return;
  2312. /* Queue the GPU write cache flushing we need. */
  2313. old_write_domain = obj->write_domain;
  2314. i915_gem_flush(dev, 0, obj->write_domain);
  2315. (void) i915_add_request(dev, NULL, obj->write_domain, obj_priv->ring);
  2316. BUG_ON(obj->write_domain);
  2317. trace_i915_gem_object_change_domain(obj,
  2318. obj->read_domains,
  2319. old_write_domain);
  2320. }
  2321. /** Flushes the GTT write domain for the object if it's dirty. */
  2322. static void
  2323. i915_gem_object_flush_gtt_write_domain(struct drm_gem_object *obj)
  2324. {
  2325. uint32_t old_write_domain;
  2326. if (obj->write_domain != I915_GEM_DOMAIN_GTT)
  2327. return;
  2328. /* No actual flushing is required for the GTT write domain. Writes
  2329. * to it immediately go to main memory as far as we know, so there's
  2330. * no chipset flush. It also doesn't land in render cache.
  2331. */
  2332. old_write_domain = obj->write_domain;
  2333. obj->write_domain = 0;
  2334. trace_i915_gem_object_change_domain(obj,
  2335. obj->read_domains,
  2336. old_write_domain);
  2337. }
  2338. /** Flushes the CPU write domain for the object if it's dirty. */
  2339. static void
  2340. i915_gem_object_flush_cpu_write_domain(struct drm_gem_object *obj)
  2341. {
  2342. struct drm_device *dev = obj->dev;
  2343. uint32_t old_write_domain;
  2344. if (obj->write_domain != I915_GEM_DOMAIN_CPU)
  2345. return;
  2346. i915_gem_clflush_object(obj);
  2347. drm_agp_chipset_flush(dev);
  2348. old_write_domain = obj->write_domain;
  2349. obj->write_domain = 0;
  2350. trace_i915_gem_object_change_domain(obj,
  2351. obj->read_domains,
  2352. old_write_domain);
  2353. }
  2354. void
  2355. i915_gem_object_flush_write_domain(struct drm_gem_object *obj)
  2356. {
  2357. switch (obj->write_domain) {
  2358. case I915_GEM_DOMAIN_GTT:
  2359. i915_gem_object_flush_gtt_write_domain(obj);
  2360. break;
  2361. case I915_GEM_DOMAIN_CPU:
  2362. i915_gem_object_flush_cpu_write_domain(obj);
  2363. break;
  2364. default:
  2365. i915_gem_object_flush_gpu_write_domain(obj);
  2366. break;
  2367. }
  2368. }
  2369. /**
  2370. * Moves a single object to the GTT read, and possibly write domain.
  2371. *
  2372. * This function returns when the move is complete, including waiting on
  2373. * flushes to occur.
  2374. */
  2375. int
  2376. i915_gem_object_set_to_gtt_domain(struct drm_gem_object *obj, int write)
  2377. {
  2378. struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
  2379. uint32_t old_write_domain, old_read_domains;
  2380. int ret;
  2381. /* Not valid to be called on unbound objects. */
  2382. if (obj_priv->gtt_space == NULL)
  2383. return -EINVAL;
  2384. i915_gem_object_flush_gpu_write_domain(obj);
  2385. /* Wait on any GPU rendering and flushing to occur. */
  2386. ret = i915_gem_object_wait_rendering(obj);
  2387. if (ret != 0)
  2388. return ret;
  2389. old_write_domain = obj->write_domain;
  2390. old_read_domains = obj->read_domains;
  2391. /* If we're writing through the GTT domain, then CPU and GPU caches
  2392. * will need to be invalidated at next use.
  2393. */
  2394. if (write)
  2395. obj->read_domains &= I915_GEM_DOMAIN_GTT;
  2396. i915_gem_object_flush_cpu_write_domain(obj);
  2397. /* It should now be out of any other write domains, and we can update
  2398. * the domain values for our changes.
  2399. */
  2400. BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_GTT) != 0);
  2401. obj->read_domains |= I915_GEM_DOMAIN_GTT;
  2402. if (write) {
  2403. obj->write_domain = I915_GEM_DOMAIN_GTT;
  2404. obj_priv->dirty = 1;
  2405. }
  2406. trace_i915_gem_object_change_domain(obj,
  2407. old_read_domains,
  2408. old_write_domain);
  2409. return 0;
  2410. }
  2411. /*
  2412. * Prepare buffer for display plane. Use uninterruptible for possible flush
  2413. * wait, as in modesetting process we're not supposed to be interrupted.
  2414. */
  2415. int
  2416. i915_gem_object_set_to_display_plane(struct drm_gem_object *obj)
  2417. {
  2418. struct drm_device *dev = obj->dev;
  2419. struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
  2420. uint32_t old_write_domain, old_read_domains;
  2421. int ret;
  2422. /* Not valid to be called on unbound objects. */
  2423. if (obj_priv->gtt_space == NULL)
  2424. return -EINVAL;
  2425. i915_gem_object_flush_gpu_write_domain(obj);
  2426. /* Wait on any GPU rendering and flushing to occur. */
  2427. if (obj_priv->active) {
  2428. #if WATCH_BUF
  2429. DRM_INFO("%s: object %p wait for seqno %08x\n",
  2430. __func__, obj, obj_priv->last_rendering_seqno);
  2431. #endif
  2432. ret = i915_do_wait_request(dev,
  2433. obj_priv->last_rendering_seqno,
  2434. 0,
  2435. obj_priv->ring);
  2436. if (ret != 0)
  2437. return ret;
  2438. }
  2439. i915_gem_object_flush_cpu_write_domain(obj);
  2440. old_write_domain = obj->write_domain;
  2441. old_read_domains = obj->read_domains;
  2442. /* It should now be out of any other write domains, and we can update
  2443. * the domain values for our changes.
  2444. */
  2445. BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_GTT) != 0);
  2446. obj->read_domains = I915_GEM_DOMAIN_GTT;
  2447. obj->write_domain = I915_GEM_DOMAIN_GTT;
  2448. obj_priv->dirty = 1;
  2449. trace_i915_gem_object_change_domain(obj,
  2450. old_read_domains,
  2451. old_write_domain);
  2452. return 0;
  2453. }
  2454. /**
  2455. * Moves a single object to the CPU read, and possibly write domain.
  2456. *
  2457. * This function returns when the move is complete, including waiting on
  2458. * flushes to occur.
  2459. */
  2460. static int
  2461. i915_gem_object_set_to_cpu_domain(struct drm_gem_object *obj, int write)
  2462. {
  2463. uint32_t old_write_domain, old_read_domains;
  2464. int ret;
  2465. i915_gem_object_flush_gpu_write_domain(obj);
  2466. /* Wait on any GPU rendering and flushing to occur. */
  2467. ret = i915_gem_object_wait_rendering(obj);
  2468. if (ret != 0)
  2469. return ret;
  2470. i915_gem_object_flush_gtt_write_domain(obj);
  2471. /* If we have a partially-valid cache of the object in the CPU,
  2472. * finish invalidating it and free the per-page flags.
  2473. */
  2474. i915_gem_object_set_to_full_cpu_read_domain(obj);
  2475. old_write_domain = obj->write_domain;
  2476. old_read_domains = obj->read_domains;
  2477. /* Flush the CPU cache if it's still invalid. */
  2478. if ((obj->read_domains & I915_GEM_DOMAIN_CPU) == 0) {
  2479. i915_gem_clflush_object(obj);
  2480. obj->read_domains |= I915_GEM_DOMAIN_CPU;
  2481. }
  2482. /* It should now be out of any other write domains, and we can update
  2483. * the domain values for our changes.
  2484. */
  2485. BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
  2486. /* If we're writing through the CPU, then the GPU read domains will
  2487. * need to be invalidated at next use.
  2488. */
  2489. if (write) {
  2490. obj->read_domains &= I915_GEM_DOMAIN_CPU;
  2491. obj->write_domain = I915_GEM_DOMAIN_CPU;
  2492. }
  2493. trace_i915_gem_object_change_domain(obj,
  2494. old_read_domains,
  2495. old_write_domain);
  2496. return 0;
  2497. }
  2498. /*
  2499. * Set the next domain for the specified object. This
  2500. * may not actually perform the necessary flushing/invaliding though,
  2501. * as that may want to be batched with other set_domain operations
  2502. *
  2503. * This is (we hope) the only really tricky part of gem. The goal
  2504. * is fairly simple -- track which caches hold bits of the object
  2505. * and make sure they remain coherent. A few concrete examples may
  2506. * help to explain how it works. For shorthand, we use the notation
  2507. * (read_domains, write_domain), e.g. (CPU, CPU) to indicate the
  2508. * a pair of read and write domain masks.
  2509. *
  2510. * Case 1: the batch buffer
  2511. *
  2512. * 1. Allocated
  2513. * 2. Written by CPU
  2514. * 3. Mapped to GTT
  2515. * 4. Read by GPU
  2516. * 5. Unmapped from GTT
  2517. * 6. Freed
  2518. *
  2519. * Let's take these a step at a time
  2520. *
  2521. * 1. Allocated
  2522. * Pages allocated from the kernel may still have
  2523. * cache contents, so we set them to (CPU, CPU) always.
  2524. * 2. Written by CPU (using pwrite)
  2525. * The pwrite function calls set_domain (CPU, CPU) and
  2526. * this function does nothing (as nothing changes)
  2527. * 3. Mapped by GTT
  2528. * This function asserts that the object is not
  2529. * currently in any GPU-based read or write domains
  2530. * 4. Read by GPU
  2531. * i915_gem_execbuffer calls set_domain (COMMAND, 0).
  2532. * As write_domain is zero, this function adds in the
  2533. * current read domains (CPU+COMMAND, 0).
  2534. * flush_domains is set to CPU.
  2535. * invalidate_domains is set to COMMAND
  2536. * clflush is run to get data out of the CPU caches
  2537. * then i915_dev_set_domain calls i915_gem_flush to
  2538. * emit an MI_FLUSH and drm_agp_chipset_flush
  2539. * 5. Unmapped from GTT
  2540. * i915_gem_object_unbind calls set_domain (CPU, CPU)
  2541. * flush_domains and invalidate_domains end up both zero
  2542. * so no flushing/invalidating happens
  2543. * 6. Freed
  2544. * yay, done
  2545. *
  2546. * Case 2: The shared render buffer
  2547. *
  2548. * 1. Allocated
  2549. * 2. Mapped to GTT
  2550. * 3. Read/written by GPU
  2551. * 4. set_domain to (CPU,CPU)
  2552. * 5. Read/written by CPU
  2553. * 6. Read/written by GPU
  2554. *
  2555. * 1. Allocated
  2556. * Same as last example, (CPU, CPU)
  2557. * 2. Mapped to GTT
  2558. * Nothing changes (assertions find that it is not in the GPU)
  2559. * 3. Read/written by GPU
  2560. * execbuffer calls set_domain (RENDER, RENDER)
  2561. * flush_domains gets CPU
  2562. * invalidate_domains gets GPU
  2563. * clflush (obj)
  2564. * MI_FLUSH and drm_agp_chipset_flush
  2565. * 4. set_domain (CPU, CPU)
  2566. * flush_domains gets GPU
  2567. * invalidate_domains gets CPU
  2568. * wait_rendering (obj) to make sure all drawing is complete.
  2569. * This will include an MI_FLUSH to get the data from GPU
  2570. * to memory
  2571. * clflush (obj) to invalidate the CPU cache
  2572. * Another MI_FLUSH in i915_gem_flush (eliminate this somehow?)
  2573. * 5. Read/written by CPU
  2574. * cache lines are loaded and dirtied
  2575. * 6. Read written by GPU
  2576. * Same as last GPU access
  2577. *
  2578. * Case 3: The constant buffer
  2579. *
  2580. * 1. Allocated
  2581. * 2. Written by CPU
  2582. * 3. Read by GPU
  2583. * 4. Updated (written) by CPU again
  2584. * 5. Read by GPU
  2585. *
  2586. * 1. Allocated
  2587. * (CPU, CPU)
  2588. * 2. Written by CPU
  2589. * (CPU, CPU)
  2590. * 3. Read by GPU
  2591. * (CPU+RENDER, 0)
  2592. * flush_domains = CPU
  2593. * invalidate_domains = RENDER
  2594. * clflush (obj)
  2595. * MI_FLUSH
  2596. * drm_agp_chipset_flush
  2597. * 4. Updated (written) by CPU again
  2598. * (CPU, CPU)
  2599. * flush_domains = 0 (no previous write domain)
  2600. * invalidate_domains = 0 (no new read domains)
  2601. * 5. Read by GPU
  2602. * (CPU+RENDER, 0)
  2603. * flush_domains = CPU
  2604. * invalidate_domains = RENDER
  2605. * clflush (obj)
  2606. * MI_FLUSH
  2607. * drm_agp_chipset_flush
  2608. */
  2609. static void
  2610. i915_gem_object_set_to_gpu_domain(struct drm_gem_object *obj)
  2611. {
  2612. struct drm_device *dev = obj->dev;
  2613. struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
  2614. uint32_t invalidate_domains = 0;
  2615. uint32_t flush_domains = 0;
  2616. uint32_t old_read_domains;
  2617. BUG_ON(obj->pending_read_domains & I915_GEM_DOMAIN_CPU);
  2618. BUG_ON(obj->pending_write_domain == I915_GEM_DOMAIN_CPU);
  2619. intel_mark_busy(dev, obj);
  2620. #if WATCH_BUF
  2621. DRM_INFO("%s: object %p read %08x -> %08x write %08x -> %08x\n",
  2622. __func__, obj,
  2623. obj->read_domains, obj->pending_read_domains,
  2624. obj->write_domain, obj->pending_write_domain);
  2625. #endif
  2626. /*
  2627. * If the object isn't moving to a new write domain,
  2628. * let the object stay in multiple read domains
  2629. */
  2630. if (obj->pending_write_domain == 0)
  2631. obj->pending_read_domains |= obj->read_domains;
  2632. else
  2633. obj_priv->dirty = 1;
  2634. /*
  2635. * Flush the current write domain if
  2636. * the new read domains don't match. Invalidate
  2637. * any read domains which differ from the old
  2638. * write domain
  2639. */
  2640. if (obj->write_domain &&
  2641. obj->write_domain != obj->pending_read_domains) {
  2642. flush_domains |= obj->write_domain;
  2643. invalidate_domains |=
  2644. obj->pending_read_domains & ~obj->write_domain;
  2645. }
  2646. /*
  2647. * Invalidate any read caches which may have
  2648. * stale data. That is, any new read domains.
  2649. */
  2650. invalidate_domains |= obj->pending_read_domains & ~obj->read_domains;
  2651. if ((flush_domains | invalidate_domains) & I915_GEM_DOMAIN_CPU) {
  2652. #if WATCH_BUF
  2653. DRM_INFO("%s: CPU domain flush %08x invalidate %08x\n",
  2654. __func__, flush_domains, invalidate_domains);
  2655. #endif
  2656. i915_gem_clflush_object(obj);
  2657. }
  2658. old_read_domains = obj->read_domains;
  2659. /* The actual obj->write_domain will be updated with
  2660. * pending_write_domain after we emit the accumulated flush for all
  2661. * of our domain changes in execbuffers (which clears objects'
  2662. * write_domains). So if we have a current write domain that we
  2663. * aren't changing, set pending_write_domain to that.
  2664. */
  2665. if (flush_domains == 0 && obj->pending_write_domain == 0)
  2666. obj->pending_write_domain = obj->write_domain;
  2667. obj->read_domains = obj->pending_read_domains;
  2668. dev->invalidate_domains |= invalidate_domains;
  2669. dev->flush_domains |= flush_domains;
  2670. #if WATCH_BUF
  2671. DRM_INFO("%s: read %08x write %08x invalidate %08x flush %08x\n",
  2672. __func__,
  2673. obj->read_domains, obj->write_domain,
  2674. dev->invalidate_domains, dev->flush_domains);
  2675. #endif
  2676. trace_i915_gem_object_change_domain(obj,
  2677. old_read_domains,
  2678. obj->write_domain);
  2679. }
  2680. /**
  2681. * Moves the object from a partially CPU read to a full one.
  2682. *
  2683. * Note that this only resolves i915_gem_object_set_cpu_read_domain_range(),
  2684. * and doesn't handle transitioning from !(read_domains & I915_GEM_DOMAIN_CPU).
  2685. */
  2686. static void
  2687. i915_gem_object_set_to_full_cpu_read_domain(struct drm_gem_object *obj)
  2688. {
  2689. struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
  2690. if (!obj_priv->page_cpu_valid)
  2691. return;
  2692. /* If we're partially in the CPU read domain, finish moving it in.
  2693. */
  2694. if (obj->read_domains & I915_GEM_DOMAIN_CPU) {
  2695. int i;
  2696. for (i = 0; i <= (obj->size - 1) / PAGE_SIZE; i++) {
  2697. if (obj_priv->page_cpu_valid[i])
  2698. continue;
  2699. drm_clflush_pages(obj_priv->pages + i, 1);
  2700. }
  2701. }
  2702. /* Free the page_cpu_valid mappings which are now stale, whether
  2703. * or not we've got I915_GEM_DOMAIN_CPU.
  2704. */
  2705. kfree(obj_priv->page_cpu_valid);
  2706. obj_priv->page_cpu_valid = NULL;
  2707. }
  2708. /**
  2709. * Set the CPU read domain on a range of the object.
  2710. *
  2711. * The object ends up with I915_GEM_DOMAIN_CPU in its read flags although it's
  2712. * not entirely valid. The page_cpu_valid member of the object flags which
  2713. * pages have been flushed, and will be respected by
  2714. * i915_gem_object_set_to_cpu_domain() if it's called on to get a valid mapping
  2715. * of the whole object.
  2716. *
  2717. * This function returns when the move is complete, including waiting on
  2718. * flushes to occur.
  2719. */
  2720. static int
  2721. i915_gem_object_set_cpu_read_domain_range(struct drm_gem_object *obj,
  2722. uint64_t offset, uint64_t size)
  2723. {
  2724. struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
  2725. uint32_t old_read_domains;
  2726. int i, ret;
  2727. if (offset == 0 && size == obj->size)
  2728. return i915_gem_object_set_to_cpu_domain(obj, 0);
  2729. i915_gem_object_flush_gpu_write_domain(obj);
  2730. /* Wait on any GPU rendering and flushing to occur. */
  2731. ret = i915_gem_object_wait_rendering(obj);
  2732. if (ret != 0)
  2733. return ret;
  2734. i915_gem_object_flush_gtt_write_domain(obj);
  2735. /* If we're already fully in the CPU read domain, we're done. */
  2736. if (obj_priv->page_cpu_valid == NULL &&
  2737. (obj->read_domains & I915_GEM_DOMAIN_CPU) != 0)
  2738. return 0;
  2739. /* Otherwise, create/clear the per-page CPU read domain flag if we're
  2740. * newly adding I915_GEM_DOMAIN_CPU
  2741. */
  2742. if (obj_priv->page_cpu_valid == NULL) {
  2743. obj_priv->page_cpu_valid = kzalloc(obj->size / PAGE_SIZE,
  2744. GFP_KERNEL);
  2745. if (obj_priv->page_cpu_valid == NULL)
  2746. return -ENOMEM;
  2747. } else if ((obj->read_domains & I915_GEM_DOMAIN_CPU) == 0)
  2748. memset(obj_priv->page_cpu_valid, 0, obj->size / PAGE_SIZE);
  2749. /* Flush the cache on any pages that are still invalid from the CPU's
  2750. * perspective.
  2751. */
  2752. for (i = offset / PAGE_SIZE; i <= (offset + size - 1) / PAGE_SIZE;
  2753. i++) {
  2754. if (obj_priv->page_cpu_valid[i])
  2755. continue;
  2756. drm_clflush_pages(obj_priv->pages + i, 1);
  2757. obj_priv->page_cpu_valid[i] = 1;
  2758. }
  2759. /* It should now be out of any other write domains, and we can update
  2760. * the domain values for our changes.
  2761. */
  2762. BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
  2763. old_read_domains = obj->read_domains;
  2764. obj->read_domains |= I915_GEM_DOMAIN_CPU;
  2765. trace_i915_gem_object_change_domain(obj,
  2766. old_read_domains,
  2767. obj->write_domain);
  2768. return 0;
  2769. }
  2770. /**
  2771. * Pin an object to the GTT and evaluate the relocations landing in it.
  2772. */
  2773. static int
  2774. i915_gem_object_pin_and_relocate(struct drm_gem_object *obj,
  2775. struct drm_file *file_priv,
  2776. struct drm_i915_gem_exec_object2 *entry,
  2777. struct drm_i915_gem_relocation_entry *relocs)
  2778. {
  2779. struct drm_device *dev = obj->dev;
  2780. drm_i915_private_t *dev_priv = dev->dev_private;
  2781. struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
  2782. int i, ret;
  2783. void __iomem *reloc_page;
  2784. bool need_fence;
  2785. need_fence = entry->flags & EXEC_OBJECT_NEEDS_FENCE &&
  2786. obj_priv->tiling_mode != I915_TILING_NONE;
  2787. /* Check fence reg constraints and rebind if necessary */
  2788. if (need_fence &&
  2789. !i915_gem_object_fence_offset_ok(obj,
  2790. obj_priv->tiling_mode)) {
  2791. ret = i915_gem_object_unbind(obj);
  2792. if (ret)
  2793. return ret;
  2794. }
  2795. /* Choose the GTT offset for our buffer and put it there. */
  2796. ret = i915_gem_object_pin(obj, (uint32_t) entry->alignment);
  2797. if (ret)
  2798. return ret;
  2799. /*
  2800. * Pre-965 chips need a fence register set up in order to
  2801. * properly handle blits to/from tiled surfaces.
  2802. */
  2803. if (need_fence) {
  2804. ret = i915_gem_object_get_fence_reg(obj);
  2805. if (ret != 0) {
  2806. i915_gem_object_unpin(obj);
  2807. return ret;
  2808. }
  2809. }
  2810. entry->offset = obj_priv->gtt_offset;
  2811. /* Apply the relocations, using the GTT aperture to avoid cache
  2812. * flushing requirements.
  2813. */
  2814. for (i = 0; i < entry->relocation_count; i++) {
  2815. struct drm_i915_gem_relocation_entry *reloc= &relocs[i];
  2816. struct drm_gem_object *target_obj;
  2817. struct drm_i915_gem_object *target_obj_priv;
  2818. uint32_t reloc_val, reloc_offset;
  2819. uint32_t __iomem *reloc_entry;
  2820. target_obj = drm_gem_object_lookup(obj->dev, file_priv,
  2821. reloc->target_handle);
  2822. if (target_obj == NULL) {
  2823. i915_gem_object_unpin(obj);
  2824. return -EBADF;
  2825. }
  2826. target_obj_priv = to_intel_bo(target_obj);
  2827. #if WATCH_RELOC
  2828. DRM_INFO("%s: obj %p offset %08x target %d "
  2829. "read %08x write %08x gtt %08x "
  2830. "presumed %08x delta %08x\n",
  2831. __func__,
  2832. obj,
  2833. (int) reloc->offset,
  2834. (int) reloc->target_handle,
  2835. (int) reloc->read_domains,
  2836. (int) reloc->write_domain,
  2837. (int) target_obj_priv->gtt_offset,
  2838. (int) reloc->presumed_offset,
  2839. reloc->delta);
  2840. #endif
  2841. /* The target buffer should have appeared before us in the
  2842. * exec_object list, so it should have a GTT space bound by now.
  2843. */
  2844. if (target_obj_priv->gtt_space == NULL) {
  2845. DRM_ERROR("No GTT space found for object %d\n",
  2846. reloc->target_handle);
  2847. drm_gem_object_unreference(target_obj);
  2848. i915_gem_object_unpin(obj);
  2849. return -EINVAL;
  2850. }
  2851. /* Validate that the target is in a valid r/w GPU domain */
  2852. if (reloc->write_domain & (reloc->write_domain - 1)) {
  2853. DRM_ERROR("reloc with multiple write domains: "
  2854. "obj %p target %d offset %d "
  2855. "read %08x write %08x",
  2856. obj, reloc->target_handle,
  2857. (int) reloc->offset,
  2858. reloc->read_domains,
  2859. reloc->write_domain);
  2860. return -EINVAL;
  2861. }
  2862. if (reloc->write_domain & I915_GEM_DOMAIN_CPU ||
  2863. reloc->read_domains & I915_GEM_DOMAIN_CPU) {
  2864. DRM_ERROR("reloc with read/write CPU domains: "
  2865. "obj %p target %d offset %d "
  2866. "read %08x write %08x",
  2867. obj, reloc->target_handle,
  2868. (int) reloc->offset,
  2869. reloc->read_domains,
  2870. reloc->write_domain);
  2871. drm_gem_object_unreference(target_obj);
  2872. i915_gem_object_unpin(obj);
  2873. return -EINVAL;
  2874. }
  2875. if (reloc->write_domain && target_obj->pending_write_domain &&
  2876. reloc->write_domain != target_obj->pending_write_domain) {
  2877. DRM_ERROR("Write domain conflict: "
  2878. "obj %p target %d offset %d "
  2879. "new %08x old %08x\n",
  2880. obj, reloc->target_handle,
  2881. (int) reloc->offset,
  2882. reloc->write_domain,
  2883. target_obj->pending_write_domain);
  2884. drm_gem_object_unreference(target_obj);
  2885. i915_gem_object_unpin(obj);
  2886. return -EINVAL;
  2887. }
  2888. target_obj->pending_read_domains |= reloc->read_domains;
  2889. target_obj->pending_write_domain |= reloc->write_domain;
  2890. /* If the relocation already has the right value in it, no
  2891. * more work needs to be done.
  2892. */
  2893. if (target_obj_priv->gtt_offset == reloc->presumed_offset) {
  2894. drm_gem_object_unreference(target_obj);
  2895. continue;
  2896. }
  2897. /* Check that the relocation address is valid... */
  2898. if (reloc->offset > obj->size - 4) {
  2899. DRM_ERROR("Relocation beyond object bounds: "
  2900. "obj %p target %d offset %d size %d.\n",
  2901. obj, reloc->target_handle,
  2902. (int) reloc->offset, (int) obj->size);
  2903. drm_gem_object_unreference(target_obj);
  2904. i915_gem_object_unpin(obj);
  2905. return -EINVAL;
  2906. }
  2907. if (reloc->offset & 3) {
  2908. DRM_ERROR("Relocation not 4-byte aligned: "
  2909. "obj %p target %d offset %d.\n",
  2910. obj, reloc->target_handle,
  2911. (int) reloc->offset);
  2912. drm_gem_object_unreference(target_obj);
  2913. i915_gem_object_unpin(obj);
  2914. return -EINVAL;
  2915. }
  2916. /* and points to somewhere within the target object. */
  2917. if (reloc->delta >= target_obj->size) {
  2918. DRM_ERROR("Relocation beyond target object bounds: "
  2919. "obj %p target %d delta %d size %d.\n",
  2920. obj, reloc->target_handle,
  2921. (int) reloc->delta, (int) target_obj->size);
  2922. drm_gem_object_unreference(target_obj);
  2923. i915_gem_object_unpin(obj);
  2924. return -EINVAL;
  2925. }
  2926. ret = i915_gem_object_set_to_gtt_domain(obj, 1);
  2927. if (ret != 0) {
  2928. drm_gem_object_unreference(target_obj);
  2929. i915_gem_object_unpin(obj);
  2930. return -EINVAL;
  2931. }
  2932. /* Map the page containing the relocation we're going to
  2933. * perform.
  2934. */
  2935. reloc_offset = obj_priv->gtt_offset + reloc->offset;
  2936. reloc_page = io_mapping_map_atomic_wc(dev_priv->mm.gtt_mapping,
  2937. (reloc_offset &
  2938. ~(PAGE_SIZE - 1)));
  2939. reloc_entry = (uint32_t __iomem *)(reloc_page +
  2940. (reloc_offset & (PAGE_SIZE - 1)));
  2941. reloc_val = target_obj_priv->gtt_offset + reloc->delta;
  2942. #if WATCH_BUF
  2943. DRM_INFO("Applied relocation: %p@0x%08x %08x -> %08x\n",
  2944. obj, (unsigned int) reloc->offset,
  2945. readl(reloc_entry), reloc_val);
  2946. #endif
  2947. writel(reloc_val, reloc_entry);
  2948. io_mapping_unmap_atomic(reloc_page);
  2949. /* The updated presumed offset for this entry will be
  2950. * copied back out to the user.
  2951. */
  2952. reloc->presumed_offset = target_obj_priv->gtt_offset;
  2953. drm_gem_object_unreference(target_obj);
  2954. }
  2955. #if WATCH_BUF
  2956. if (0)
  2957. i915_gem_dump_object(obj, 128, __func__, ~0);
  2958. #endif
  2959. return 0;
  2960. }
  2961. /* Throttle our rendering by waiting until the ring has completed our requests
  2962. * emitted over 20 msec ago.
  2963. *
  2964. * Note that if we were to use the current jiffies each time around the loop,
  2965. * we wouldn't escape the function with any frames outstanding if the time to
  2966. * render a frame was over 20ms.
  2967. *
  2968. * This should get us reasonable parallelism between CPU and GPU but also
  2969. * relatively low latency when blocking on a particular request to finish.
  2970. */
  2971. static int
  2972. i915_gem_ring_throttle(struct drm_device *dev, struct drm_file *file_priv)
  2973. {
  2974. struct drm_i915_file_private *i915_file_priv = file_priv->driver_priv;
  2975. int ret = 0;
  2976. unsigned long recent_enough = jiffies - msecs_to_jiffies(20);
  2977. mutex_lock(&dev->struct_mutex);
  2978. while (!list_empty(&i915_file_priv->mm.request_list)) {
  2979. struct drm_i915_gem_request *request;
  2980. request = list_first_entry(&i915_file_priv->mm.request_list,
  2981. struct drm_i915_gem_request,
  2982. client_list);
  2983. if (time_after_eq(request->emitted_jiffies, recent_enough))
  2984. break;
  2985. ret = i915_wait_request(dev, request->seqno, request->ring);
  2986. if (ret != 0)
  2987. break;
  2988. }
  2989. mutex_unlock(&dev->struct_mutex);
  2990. return ret;
  2991. }
  2992. static int
  2993. i915_gem_get_relocs_from_user(struct drm_i915_gem_exec_object2 *exec_list,
  2994. uint32_t buffer_count,
  2995. struct drm_i915_gem_relocation_entry **relocs)
  2996. {
  2997. uint32_t reloc_count = 0, reloc_index = 0, i;
  2998. int ret;
  2999. *relocs = NULL;
  3000. for (i = 0; i < buffer_count; i++) {
  3001. if (reloc_count + exec_list[i].relocation_count < reloc_count)
  3002. return -EINVAL;
  3003. reloc_count += exec_list[i].relocation_count;
  3004. }
  3005. *relocs = drm_calloc_large(reloc_count, sizeof(**relocs));
  3006. if (*relocs == NULL) {
  3007. DRM_ERROR("failed to alloc relocs, count %d\n", reloc_count);
  3008. return -ENOMEM;
  3009. }
  3010. for (i = 0; i < buffer_count; i++) {
  3011. struct drm_i915_gem_relocation_entry __user *user_relocs;
  3012. user_relocs = (void __user *)(uintptr_t)exec_list[i].relocs_ptr;
  3013. ret = copy_from_user(&(*relocs)[reloc_index],
  3014. user_relocs,
  3015. exec_list[i].relocation_count *
  3016. sizeof(**relocs));
  3017. if (ret != 0) {
  3018. drm_free_large(*relocs);
  3019. *relocs = NULL;
  3020. return -EFAULT;
  3021. }
  3022. reloc_index += exec_list[i].relocation_count;
  3023. }
  3024. return 0;
  3025. }
  3026. static int
  3027. i915_gem_put_relocs_to_user(struct drm_i915_gem_exec_object2 *exec_list,
  3028. uint32_t buffer_count,
  3029. struct drm_i915_gem_relocation_entry *relocs)
  3030. {
  3031. uint32_t reloc_count = 0, i;
  3032. int ret = 0;
  3033. if (relocs == NULL)
  3034. return 0;
  3035. for (i = 0; i < buffer_count; i++) {
  3036. struct drm_i915_gem_relocation_entry __user *user_relocs;
  3037. int unwritten;
  3038. user_relocs = (void __user *)(uintptr_t)exec_list[i].relocs_ptr;
  3039. unwritten = copy_to_user(user_relocs,
  3040. &relocs[reloc_count],
  3041. exec_list[i].relocation_count *
  3042. sizeof(*relocs));
  3043. if (unwritten) {
  3044. ret = -EFAULT;
  3045. goto err;
  3046. }
  3047. reloc_count += exec_list[i].relocation_count;
  3048. }
  3049. err:
  3050. drm_free_large(relocs);
  3051. return ret;
  3052. }
  3053. static int
  3054. i915_gem_check_execbuffer (struct drm_i915_gem_execbuffer2 *exec,
  3055. uint64_t exec_offset)
  3056. {
  3057. uint32_t exec_start, exec_len;
  3058. exec_start = (uint32_t) exec_offset + exec->batch_start_offset;
  3059. exec_len = (uint32_t) exec->batch_len;
  3060. if ((exec_start | exec_len) & 0x7)
  3061. return -EINVAL;
  3062. if (!exec_start)
  3063. return -EINVAL;
  3064. return 0;
  3065. }
  3066. static int
  3067. i915_gem_wait_for_pending_flip(struct drm_device *dev,
  3068. struct drm_gem_object **object_list,
  3069. int count)
  3070. {
  3071. drm_i915_private_t *dev_priv = dev->dev_private;
  3072. struct drm_i915_gem_object *obj_priv;
  3073. DEFINE_WAIT(wait);
  3074. int i, ret = 0;
  3075. for (;;) {
  3076. prepare_to_wait(&dev_priv->pending_flip_queue,
  3077. &wait, TASK_INTERRUPTIBLE);
  3078. for (i = 0; i < count; i++) {
  3079. obj_priv = to_intel_bo(object_list[i]);
  3080. if (atomic_read(&obj_priv->pending_flip) > 0)
  3081. break;
  3082. }
  3083. if (i == count)
  3084. break;
  3085. if (!signal_pending(current)) {
  3086. mutex_unlock(&dev->struct_mutex);
  3087. schedule();
  3088. mutex_lock(&dev->struct_mutex);
  3089. continue;
  3090. }
  3091. ret = -ERESTARTSYS;
  3092. break;
  3093. }
  3094. finish_wait(&dev_priv->pending_flip_queue, &wait);
  3095. return ret;
  3096. }
  3097. int
  3098. i915_gem_do_execbuffer(struct drm_device *dev, void *data,
  3099. struct drm_file *file_priv,
  3100. struct drm_i915_gem_execbuffer2 *args,
  3101. struct drm_i915_gem_exec_object2 *exec_list)
  3102. {
  3103. drm_i915_private_t *dev_priv = dev->dev_private;
  3104. struct drm_gem_object **object_list = NULL;
  3105. struct drm_gem_object *batch_obj;
  3106. struct drm_i915_gem_object *obj_priv;
  3107. struct drm_clip_rect *cliprects = NULL;
  3108. struct drm_i915_gem_relocation_entry *relocs = NULL;
  3109. int ret = 0, ret2, i, pinned = 0;
  3110. uint64_t exec_offset;
  3111. uint32_t seqno, flush_domains, reloc_index;
  3112. int pin_tries, flips;
  3113. struct intel_ring_buffer *ring = NULL;
  3114. #if WATCH_EXEC
  3115. DRM_INFO("buffers_ptr %d buffer_count %d len %08x\n",
  3116. (int) args->buffers_ptr, args->buffer_count, args->batch_len);
  3117. #endif
  3118. if (args->flags & I915_EXEC_BSD) {
  3119. if (!HAS_BSD(dev)) {
  3120. DRM_ERROR("execbuf with wrong flag\n");
  3121. return -EINVAL;
  3122. }
  3123. ring = &dev_priv->bsd_ring;
  3124. } else {
  3125. ring = &dev_priv->render_ring;
  3126. }
  3127. if (args->buffer_count < 1) {
  3128. DRM_ERROR("execbuf with %d buffers\n", args->buffer_count);
  3129. return -EINVAL;
  3130. }
  3131. object_list = drm_malloc_ab(sizeof(*object_list), args->buffer_count);
  3132. if (object_list == NULL) {
  3133. DRM_ERROR("Failed to allocate object list for %d buffers\n",
  3134. args->buffer_count);
  3135. ret = -ENOMEM;
  3136. goto pre_mutex_err;
  3137. }
  3138. if (args->num_cliprects != 0) {
  3139. cliprects = kcalloc(args->num_cliprects, sizeof(*cliprects),
  3140. GFP_KERNEL);
  3141. if (cliprects == NULL) {
  3142. ret = -ENOMEM;
  3143. goto pre_mutex_err;
  3144. }
  3145. ret = copy_from_user(cliprects,
  3146. (struct drm_clip_rect __user *)
  3147. (uintptr_t) args->cliprects_ptr,
  3148. sizeof(*cliprects) * args->num_cliprects);
  3149. if (ret != 0) {
  3150. DRM_ERROR("copy %d cliprects failed: %d\n",
  3151. args->num_cliprects, ret);
  3152. goto pre_mutex_err;
  3153. }
  3154. }
  3155. ret = i915_gem_get_relocs_from_user(exec_list, args->buffer_count,
  3156. &relocs);
  3157. if (ret != 0)
  3158. goto pre_mutex_err;
  3159. mutex_lock(&dev->struct_mutex);
  3160. i915_verify_inactive(dev, __FILE__, __LINE__);
  3161. if (atomic_read(&dev_priv->mm.wedged)) {
  3162. mutex_unlock(&dev->struct_mutex);
  3163. ret = -EIO;
  3164. goto pre_mutex_err;
  3165. }
  3166. if (dev_priv->mm.suspended) {
  3167. mutex_unlock(&dev->struct_mutex);
  3168. ret = -EBUSY;
  3169. goto pre_mutex_err;
  3170. }
  3171. /* Look up object handles */
  3172. flips = 0;
  3173. for (i = 0; i < args->buffer_count; i++) {
  3174. object_list[i] = drm_gem_object_lookup(dev, file_priv,
  3175. exec_list[i].handle);
  3176. if (object_list[i] == NULL) {
  3177. DRM_ERROR("Invalid object handle %d at index %d\n",
  3178. exec_list[i].handle, i);
  3179. /* prevent error path from reading uninitialized data */
  3180. args->buffer_count = i + 1;
  3181. ret = -EBADF;
  3182. goto err;
  3183. }
  3184. obj_priv = to_intel_bo(object_list[i]);
  3185. if (obj_priv->in_execbuffer) {
  3186. DRM_ERROR("Object %p appears more than once in object list\n",
  3187. object_list[i]);
  3188. /* prevent error path from reading uninitialized data */
  3189. args->buffer_count = i + 1;
  3190. ret = -EBADF;
  3191. goto err;
  3192. }
  3193. obj_priv->in_execbuffer = true;
  3194. flips += atomic_read(&obj_priv->pending_flip);
  3195. }
  3196. if (flips > 0) {
  3197. ret = i915_gem_wait_for_pending_flip(dev, object_list,
  3198. args->buffer_count);
  3199. if (ret)
  3200. goto err;
  3201. }
  3202. /* Pin and relocate */
  3203. for (pin_tries = 0; ; pin_tries++) {
  3204. ret = 0;
  3205. reloc_index = 0;
  3206. for (i = 0; i < args->buffer_count; i++) {
  3207. object_list[i]->pending_read_domains = 0;
  3208. object_list[i]->pending_write_domain = 0;
  3209. ret = i915_gem_object_pin_and_relocate(object_list[i],
  3210. file_priv,
  3211. &exec_list[i],
  3212. &relocs[reloc_index]);
  3213. if (ret)
  3214. break;
  3215. pinned = i + 1;
  3216. reloc_index += exec_list[i].relocation_count;
  3217. }
  3218. /* success */
  3219. if (ret == 0)
  3220. break;
  3221. /* error other than GTT full, or we've already tried again */
  3222. if (ret != -ENOSPC || pin_tries >= 1) {
  3223. if (ret != -ERESTARTSYS) {
  3224. unsigned long long total_size = 0;
  3225. int num_fences = 0;
  3226. for (i = 0; i < args->buffer_count; i++) {
  3227. obj_priv = object_list[i]->driver_private;
  3228. total_size += object_list[i]->size;
  3229. num_fences +=
  3230. exec_list[i].flags & EXEC_OBJECT_NEEDS_FENCE &&
  3231. obj_priv->tiling_mode != I915_TILING_NONE;
  3232. }
  3233. DRM_ERROR("Failed to pin buffer %d of %d, total %llu bytes, %d fences: %d\n",
  3234. pinned+1, args->buffer_count,
  3235. total_size, num_fences,
  3236. ret);
  3237. DRM_ERROR("%d objects [%d pinned], "
  3238. "%d object bytes [%d pinned], "
  3239. "%d/%d gtt bytes\n",
  3240. atomic_read(&dev->object_count),
  3241. atomic_read(&dev->pin_count),
  3242. atomic_read(&dev->object_memory),
  3243. atomic_read(&dev->pin_memory),
  3244. atomic_read(&dev->gtt_memory),
  3245. dev->gtt_total);
  3246. }
  3247. goto err;
  3248. }
  3249. /* unpin all of our buffers */
  3250. for (i = 0; i < pinned; i++)
  3251. i915_gem_object_unpin(object_list[i]);
  3252. pinned = 0;
  3253. /* evict everyone we can from the aperture */
  3254. ret = i915_gem_evict_everything(dev);
  3255. if (ret && ret != -ENOSPC)
  3256. goto err;
  3257. }
  3258. /* Set the pending read domains for the batch buffer to COMMAND */
  3259. batch_obj = object_list[args->buffer_count-1];
  3260. if (batch_obj->pending_write_domain) {
  3261. DRM_ERROR("Attempting to use self-modifying batch buffer\n");
  3262. ret = -EINVAL;
  3263. goto err;
  3264. }
  3265. batch_obj->pending_read_domains |= I915_GEM_DOMAIN_COMMAND;
  3266. /* Sanity check the batch buffer, prior to moving objects */
  3267. exec_offset = exec_list[args->buffer_count - 1].offset;
  3268. ret = i915_gem_check_execbuffer (args, exec_offset);
  3269. if (ret != 0) {
  3270. DRM_ERROR("execbuf with invalid offset/length\n");
  3271. goto err;
  3272. }
  3273. i915_verify_inactive(dev, __FILE__, __LINE__);
  3274. /* Zero the global flush/invalidate flags. These
  3275. * will be modified as new domains are computed
  3276. * for each object
  3277. */
  3278. dev->invalidate_domains = 0;
  3279. dev->flush_domains = 0;
  3280. for (i = 0; i < args->buffer_count; i++) {
  3281. struct drm_gem_object *obj = object_list[i];
  3282. /* Compute new gpu domains and update invalidate/flush */
  3283. i915_gem_object_set_to_gpu_domain(obj);
  3284. }
  3285. i915_verify_inactive(dev, __FILE__, __LINE__);
  3286. if (dev->invalidate_domains | dev->flush_domains) {
  3287. #if WATCH_EXEC
  3288. DRM_INFO("%s: invalidate_domains %08x flush_domains %08x\n",
  3289. __func__,
  3290. dev->invalidate_domains,
  3291. dev->flush_domains);
  3292. #endif
  3293. i915_gem_flush(dev,
  3294. dev->invalidate_domains,
  3295. dev->flush_domains);
  3296. if (dev->flush_domains & I915_GEM_GPU_DOMAINS) {
  3297. (void)i915_add_request(dev, file_priv,
  3298. dev->flush_domains,
  3299. &dev_priv->render_ring);
  3300. if (HAS_BSD(dev))
  3301. (void)i915_add_request(dev, file_priv,
  3302. dev->flush_domains,
  3303. &dev_priv->bsd_ring);
  3304. }
  3305. }
  3306. for (i = 0; i < args->buffer_count; i++) {
  3307. struct drm_gem_object *obj = object_list[i];
  3308. struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
  3309. uint32_t old_write_domain = obj->write_domain;
  3310. obj->write_domain = obj->pending_write_domain;
  3311. if (obj->write_domain)
  3312. list_move_tail(&obj_priv->gpu_write_list,
  3313. &dev_priv->mm.gpu_write_list);
  3314. else
  3315. list_del_init(&obj_priv->gpu_write_list);
  3316. trace_i915_gem_object_change_domain(obj,
  3317. obj->read_domains,
  3318. old_write_domain);
  3319. }
  3320. i915_verify_inactive(dev, __FILE__, __LINE__);
  3321. #if WATCH_COHERENCY
  3322. for (i = 0; i < args->buffer_count; i++) {
  3323. i915_gem_object_check_coherency(object_list[i],
  3324. exec_list[i].handle);
  3325. }
  3326. #endif
  3327. #if WATCH_EXEC
  3328. i915_gem_dump_object(batch_obj,
  3329. args->batch_len,
  3330. __func__,
  3331. ~0);
  3332. #endif
  3333. /* Exec the batchbuffer */
  3334. ret = ring->dispatch_gem_execbuffer(dev, ring, args,
  3335. cliprects, exec_offset);
  3336. if (ret) {
  3337. DRM_ERROR("dispatch failed %d\n", ret);
  3338. goto err;
  3339. }
  3340. /*
  3341. * Ensure that the commands in the batch buffer are
  3342. * finished before the interrupt fires
  3343. */
  3344. flush_domains = i915_retire_commands(dev, ring);
  3345. i915_verify_inactive(dev, __FILE__, __LINE__);
  3346. /*
  3347. * Get a seqno representing the execution of the current buffer,
  3348. * which we can wait on. We would like to mitigate these interrupts,
  3349. * likely by only creating seqnos occasionally (so that we have
  3350. * *some* interrupts representing completion of buffers that we can
  3351. * wait on when trying to clear up gtt space).
  3352. */
  3353. seqno = i915_add_request(dev, file_priv, flush_domains, ring);
  3354. BUG_ON(seqno == 0);
  3355. for (i = 0; i < args->buffer_count; i++) {
  3356. struct drm_gem_object *obj = object_list[i];
  3357. obj_priv = to_intel_bo(obj);
  3358. i915_gem_object_move_to_active(obj, seqno, ring);
  3359. #if WATCH_LRU
  3360. DRM_INFO("%s: move to exec list %p\n", __func__, obj);
  3361. #endif
  3362. }
  3363. #if WATCH_LRU
  3364. i915_dump_lru(dev, __func__);
  3365. #endif
  3366. i915_verify_inactive(dev, __FILE__, __LINE__);
  3367. err:
  3368. for (i = 0; i < pinned; i++)
  3369. i915_gem_object_unpin(object_list[i]);
  3370. for (i = 0; i < args->buffer_count; i++) {
  3371. if (object_list[i]) {
  3372. obj_priv = to_intel_bo(object_list[i]);
  3373. obj_priv->in_execbuffer = false;
  3374. }
  3375. drm_gem_object_unreference(object_list[i]);
  3376. }
  3377. mutex_unlock(&dev->struct_mutex);
  3378. pre_mutex_err:
  3379. /* Copy the updated relocations out regardless of current error
  3380. * state. Failure to update the relocs would mean that the next
  3381. * time userland calls execbuf, it would do so with presumed offset
  3382. * state that didn't match the actual object state.
  3383. */
  3384. ret2 = i915_gem_put_relocs_to_user(exec_list, args->buffer_count,
  3385. relocs);
  3386. if (ret2 != 0) {
  3387. DRM_ERROR("Failed to copy relocations back out: %d\n", ret2);
  3388. if (ret == 0)
  3389. ret = ret2;
  3390. }
  3391. drm_free_large(object_list);
  3392. kfree(cliprects);
  3393. return ret;
  3394. }
  3395. /*
  3396. * Legacy execbuffer just creates an exec2 list from the original exec object
  3397. * list array and passes it to the real function.
  3398. */
  3399. int
  3400. i915_gem_execbuffer(struct drm_device *dev, void *data,
  3401. struct drm_file *file_priv)
  3402. {
  3403. struct drm_i915_gem_execbuffer *args = data;
  3404. struct drm_i915_gem_execbuffer2 exec2;
  3405. struct drm_i915_gem_exec_object *exec_list = NULL;
  3406. struct drm_i915_gem_exec_object2 *exec2_list = NULL;
  3407. int ret, i;
  3408. #if WATCH_EXEC
  3409. DRM_INFO("buffers_ptr %d buffer_count %d len %08x\n",
  3410. (int) args->buffers_ptr, args->buffer_count, args->batch_len);
  3411. #endif
  3412. if (args->buffer_count < 1) {
  3413. DRM_ERROR("execbuf with %d buffers\n", args->buffer_count);
  3414. return -EINVAL;
  3415. }
  3416. /* Copy in the exec list from userland */
  3417. exec_list = drm_malloc_ab(sizeof(*exec_list), args->buffer_count);
  3418. exec2_list = drm_malloc_ab(sizeof(*exec2_list), args->buffer_count);
  3419. if (exec_list == NULL || exec2_list == NULL) {
  3420. DRM_ERROR("Failed to allocate exec list for %d buffers\n",
  3421. args->buffer_count);
  3422. drm_free_large(exec_list);
  3423. drm_free_large(exec2_list);
  3424. return -ENOMEM;
  3425. }
  3426. ret = copy_from_user(exec_list,
  3427. (struct drm_i915_relocation_entry __user *)
  3428. (uintptr_t) args->buffers_ptr,
  3429. sizeof(*exec_list) * args->buffer_count);
  3430. if (ret != 0) {
  3431. DRM_ERROR("copy %d exec entries failed %d\n",
  3432. args->buffer_count, ret);
  3433. drm_free_large(exec_list);
  3434. drm_free_large(exec2_list);
  3435. return -EFAULT;
  3436. }
  3437. for (i = 0; i < args->buffer_count; i++) {
  3438. exec2_list[i].handle = exec_list[i].handle;
  3439. exec2_list[i].relocation_count = exec_list[i].relocation_count;
  3440. exec2_list[i].relocs_ptr = exec_list[i].relocs_ptr;
  3441. exec2_list[i].alignment = exec_list[i].alignment;
  3442. exec2_list[i].offset = exec_list[i].offset;
  3443. if (!IS_I965G(dev))
  3444. exec2_list[i].flags = EXEC_OBJECT_NEEDS_FENCE;
  3445. else
  3446. exec2_list[i].flags = 0;
  3447. }
  3448. exec2.buffers_ptr = args->buffers_ptr;
  3449. exec2.buffer_count = args->buffer_count;
  3450. exec2.batch_start_offset = args->batch_start_offset;
  3451. exec2.batch_len = args->batch_len;
  3452. exec2.DR1 = args->DR1;
  3453. exec2.DR4 = args->DR4;
  3454. exec2.num_cliprects = args->num_cliprects;
  3455. exec2.cliprects_ptr = args->cliprects_ptr;
  3456. exec2.flags = I915_EXEC_RENDER;
  3457. ret = i915_gem_do_execbuffer(dev, data, file_priv, &exec2, exec2_list);
  3458. if (!ret) {
  3459. /* Copy the new buffer offsets back to the user's exec list. */
  3460. for (i = 0; i < args->buffer_count; i++)
  3461. exec_list[i].offset = exec2_list[i].offset;
  3462. /* ... and back out to userspace */
  3463. ret = copy_to_user((struct drm_i915_relocation_entry __user *)
  3464. (uintptr_t) args->buffers_ptr,
  3465. exec_list,
  3466. sizeof(*exec_list) * args->buffer_count);
  3467. if (ret) {
  3468. ret = -EFAULT;
  3469. DRM_ERROR("failed to copy %d exec entries "
  3470. "back to user (%d)\n",
  3471. args->buffer_count, ret);
  3472. }
  3473. }
  3474. drm_free_large(exec_list);
  3475. drm_free_large(exec2_list);
  3476. return ret;
  3477. }
  3478. int
  3479. i915_gem_execbuffer2(struct drm_device *dev, void *data,
  3480. struct drm_file *file_priv)
  3481. {
  3482. struct drm_i915_gem_execbuffer2 *args = data;
  3483. struct drm_i915_gem_exec_object2 *exec2_list = NULL;
  3484. int ret;
  3485. #if WATCH_EXEC
  3486. DRM_INFO("buffers_ptr %d buffer_count %d len %08x\n",
  3487. (int) args->buffers_ptr, args->buffer_count, args->batch_len);
  3488. #endif
  3489. if (args->buffer_count < 1) {
  3490. DRM_ERROR("execbuf2 with %d buffers\n", args->buffer_count);
  3491. return -EINVAL;
  3492. }
  3493. exec2_list = drm_malloc_ab(sizeof(*exec2_list), args->buffer_count);
  3494. if (exec2_list == NULL) {
  3495. DRM_ERROR("Failed to allocate exec list for %d buffers\n",
  3496. args->buffer_count);
  3497. return -ENOMEM;
  3498. }
  3499. ret = copy_from_user(exec2_list,
  3500. (struct drm_i915_relocation_entry __user *)
  3501. (uintptr_t) args->buffers_ptr,
  3502. sizeof(*exec2_list) * args->buffer_count);
  3503. if (ret != 0) {
  3504. DRM_ERROR("copy %d exec entries failed %d\n",
  3505. args->buffer_count, ret);
  3506. drm_free_large(exec2_list);
  3507. return -EFAULT;
  3508. }
  3509. ret = i915_gem_do_execbuffer(dev, data, file_priv, args, exec2_list);
  3510. if (!ret) {
  3511. /* Copy the new buffer offsets back to the user's exec list. */
  3512. ret = copy_to_user((struct drm_i915_relocation_entry __user *)
  3513. (uintptr_t) args->buffers_ptr,
  3514. exec2_list,
  3515. sizeof(*exec2_list) * args->buffer_count);
  3516. if (ret) {
  3517. ret = -EFAULT;
  3518. DRM_ERROR("failed to copy %d exec entries "
  3519. "back to user (%d)\n",
  3520. args->buffer_count, ret);
  3521. }
  3522. }
  3523. drm_free_large(exec2_list);
  3524. return ret;
  3525. }
  3526. int
  3527. i915_gem_object_pin(struct drm_gem_object *obj, uint32_t alignment)
  3528. {
  3529. struct drm_device *dev = obj->dev;
  3530. struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
  3531. int ret;
  3532. BUG_ON(obj_priv->pin_count == DRM_I915_GEM_OBJECT_MAX_PIN_COUNT);
  3533. i915_verify_inactive(dev, __FILE__, __LINE__);
  3534. if (obj_priv->gtt_space != NULL) {
  3535. if (alignment == 0)
  3536. alignment = i915_gem_get_gtt_alignment(obj);
  3537. if (obj_priv->gtt_offset & (alignment - 1)) {
  3538. ret = i915_gem_object_unbind(obj);
  3539. if (ret)
  3540. return ret;
  3541. }
  3542. }
  3543. if (obj_priv->gtt_space == NULL) {
  3544. ret = i915_gem_object_bind_to_gtt(obj, alignment);
  3545. if (ret)
  3546. return ret;
  3547. }
  3548. obj_priv->pin_count++;
  3549. /* If the object is not active and not pending a flush,
  3550. * remove it from the inactive list
  3551. */
  3552. if (obj_priv->pin_count == 1) {
  3553. atomic_inc(&dev->pin_count);
  3554. atomic_add(obj->size, &dev->pin_memory);
  3555. if (!obj_priv->active &&
  3556. (obj->write_domain & I915_GEM_GPU_DOMAINS) == 0 &&
  3557. !list_empty(&obj_priv->list))
  3558. list_del_init(&obj_priv->list);
  3559. }
  3560. i915_verify_inactive(dev, __FILE__, __LINE__);
  3561. return 0;
  3562. }
  3563. void
  3564. i915_gem_object_unpin(struct drm_gem_object *obj)
  3565. {
  3566. struct drm_device *dev = obj->dev;
  3567. drm_i915_private_t *dev_priv = dev->dev_private;
  3568. struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
  3569. i915_verify_inactive(dev, __FILE__, __LINE__);
  3570. obj_priv->pin_count--;
  3571. BUG_ON(obj_priv->pin_count < 0);
  3572. BUG_ON(obj_priv->gtt_space == NULL);
  3573. /* If the object is no longer pinned, and is
  3574. * neither active nor being flushed, then stick it on
  3575. * the inactive list
  3576. */
  3577. if (obj_priv->pin_count == 0) {
  3578. if (!obj_priv->active &&
  3579. (obj->write_domain & I915_GEM_GPU_DOMAINS) == 0)
  3580. list_move_tail(&obj_priv->list,
  3581. &dev_priv->mm.inactive_list);
  3582. atomic_dec(&dev->pin_count);
  3583. atomic_sub(obj->size, &dev->pin_memory);
  3584. }
  3585. i915_verify_inactive(dev, __FILE__, __LINE__);
  3586. }
  3587. int
  3588. i915_gem_pin_ioctl(struct drm_device *dev, void *data,
  3589. struct drm_file *file_priv)
  3590. {
  3591. struct drm_i915_gem_pin *args = data;
  3592. struct drm_gem_object *obj;
  3593. struct drm_i915_gem_object *obj_priv;
  3594. int ret;
  3595. mutex_lock(&dev->struct_mutex);
  3596. obj = drm_gem_object_lookup(dev, file_priv, args->handle);
  3597. if (obj == NULL) {
  3598. DRM_ERROR("Bad handle in i915_gem_pin_ioctl(): %d\n",
  3599. args->handle);
  3600. mutex_unlock(&dev->struct_mutex);
  3601. return -EBADF;
  3602. }
  3603. obj_priv = to_intel_bo(obj);
  3604. if (obj_priv->madv != I915_MADV_WILLNEED) {
  3605. DRM_ERROR("Attempting to pin a purgeable buffer\n");
  3606. drm_gem_object_unreference(obj);
  3607. mutex_unlock(&dev->struct_mutex);
  3608. return -EINVAL;
  3609. }
  3610. if (obj_priv->pin_filp != NULL && obj_priv->pin_filp != file_priv) {
  3611. DRM_ERROR("Already pinned in i915_gem_pin_ioctl(): %d\n",
  3612. args->handle);
  3613. drm_gem_object_unreference(obj);
  3614. mutex_unlock(&dev->struct_mutex);
  3615. return -EINVAL;
  3616. }
  3617. obj_priv->user_pin_count++;
  3618. obj_priv->pin_filp = file_priv;
  3619. if (obj_priv->user_pin_count == 1) {
  3620. ret = i915_gem_object_pin(obj, args->alignment);
  3621. if (ret != 0) {
  3622. drm_gem_object_unreference(obj);
  3623. mutex_unlock(&dev->struct_mutex);
  3624. return ret;
  3625. }
  3626. }
  3627. /* XXX - flush the CPU caches for pinned objects
  3628. * as the X server doesn't manage domains yet
  3629. */
  3630. i915_gem_object_flush_cpu_write_domain(obj);
  3631. args->offset = obj_priv->gtt_offset;
  3632. drm_gem_object_unreference(obj);
  3633. mutex_unlock(&dev->struct_mutex);
  3634. return 0;
  3635. }
  3636. int
  3637. i915_gem_unpin_ioctl(struct drm_device *dev, void *data,
  3638. struct drm_file *file_priv)
  3639. {
  3640. struct drm_i915_gem_pin *args = data;
  3641. struct drm_gem_object *obj;
  3642. struct drm_i915_gem_object *obj_priv;
  3643. mutex_lock(&dev->struct_mutex);
  3644. obj = drm_gem_object_lookup(dev, file_priv, args->handle);
  3645. if (obj == NULL) {
  3646. DRM_ERROR("Bad handle in i915_gem_unpin_ioctl(): %d\n",
  3647. args->handle);
  3648. mutex_unlock(&dev->struct_mutex);
  3649. return -EBADF;
  3650. }
  3651. obj_priv = to_intel_bo(obj);
  3652. if (obj_priv->pin_filp != file_priv) {
  3653. DRM_ERROR("Not pinned by caller in i915_gem_pin_ioctl(): %d\n",
  3654. args->handle);
  3655. drm_gem_object_unreference(obj);
  3656. mutex_unlock(&dev->struct_mutex);
  3657. return -EINVAL;
  3658. }
  3659. obj_priv->user_pin_count--;
  3660. if (obj_priv->user_pin_count == 0) {
  3661. obj_priv->pin_filp = NULL;
  3662. i915_gem_object_unpin(obj);
  3663. }
  3664. drm_gem_object_unreference(obj);
  3665. mutex_unlock(&dev->struct_mutex);
  3666. return 0;
  3667. }
  3668. int
  3669. i915_gem_busy_ioctl(struct drm_device *dev, void *data,
  3670. struct drm_file *file_priv)
  3671. {
  3672. struct drm_i915_gem_busy *args = data;
  3673. struct drm_gem_object *obj;
  3674. struct drm_i915_gem_object *obj_priv;
  3675. drm_i915_private_t *dev_priv = dev->dev_private;
  3676. obj = drm_gem_object_lookup(dev, file_priv, args->handle);
  3677. if (obj == NULL) {
  3678. DRM_ERROR("Bad handle in i915_gem_busy_ioctl(): %d\n",
  3679. args->handle);
  3680. return -EBADF;
  3681. }
  3682. mutex_lock(&dev->struct_mutex);
  3683. /* Update the active list for the hardware's current position.
  3684. * Otherwise this only updates on a delayed timer or when irqs are
  3685. * actually unmasked, and our working set ends up being larger than
  3686. * required.
  3687. */
  3688. i915_gem_retire_requests(dev, &dev_priv->render_ring);
  3689. if (HAS_BSD(dev))
  3690. i915_gem_retire_requests(dev, &dev_priv->bsd_ring);
  3691. obj_priv = to_intel_bo(obj);
  3692. /* Don't count being on the flushing list against the object being
  3693. * done. Otherwise, a buffer left on the flushing list but not getting
  3694. * flushed (because nobody's flushing that domain) won't ever return
  3695. * unbusy and get reused by libdrm's bo cache. The other expected
  3696. * consumer of this interface, OpenGL's occlusion queries, also specs
  3697. * that the objects get unbusy "eventually" without any interference.
  3698. */
  3699. args->busy = obj_priv->active && obj_priv->last_rendering_seqno != 0;
  3700. drm_gem_object_unreference(obj);
  3701. mutex_unlock(&dev->struct_mutex);
  3702. return 0;
  3703. }
  3704. int
  3705. i915_gem_throttle_ioctl(struct drm_device *dev, void *data,
  3706. struct drm_file *file_priv)
  3707. {
  3708. return i915_gem_ring_throttle(dev, file_priv);
  3709. }
  3710. int
  3711. i915_gem_madvise_ioctl(struct drm_device *dev, void *data,
  3712. struct drm_file *file_priv)
  3713. {
  3714. struct drm_i915_gem_madvise *args = data;
  3715. struct drm_gem_object *obj;
  3716. struct drm_i915_gem_object *obj_priv;
  3717. switch (args->madv) {
  3718. case I915_MADV_DONTNEED:
  3719. case I915_MADV_WILLNEED:
  3720. break;
  3721. default:
  3722. return -EINVAL;
  3723. }
  3724. obj = drm_gem_object_lookup(dev, file_priv, args->handle);
  3725. if (obj == NULL) {
  3726. DRM_ERROR("Bad handle in i915_gem_madvise_ioctl(): %d\n",
  3727. args->handle);
  3728. return -EBADF;
  3729. }
  3730. mutex_lock(&dev->struct_mutex);
  3731. obj_priv = to_intel_bo(obj);
  3732. if (obj_priv->pin_count) {
  3733. drm_gem_object_unreference(obj);
  3734. mutex_unlock(&dev->struct_mutex);
  3735. DRM_ERROR("Attempted i915_gem_madvise_ioctl() on a pinned object\n");
  3736. return -EINVAL;
  3737. }
  3738. if (obj_priv->madv != __I915_MADV_PURGED)
  3739. obj_priv->madv = args->madv;
  3740. /* if the object is no longer bound, discard its backing storage */
  3741. if (i915_gem_object_is_purgeable(obj_priv) &&
  3742. obj_priv->gtt_space == NULL)
  3743. i915_gem_object_truncate(obj);
  3744. args->retained = obj_priv->madv != __I915_MADV_PURGED;
  3745. drm_gem_object_unreference(obj);
  3746. mutex_unlock(&dev->struct_mutex);
  3747. return 0;
  3748. }
  3749. struct drm_gem_object * i915_gem_alloc_object(struct drm_device *dev,
  3750. size_t size)
  3751. {
  3752. struct drm_i915_gem_object *obj;
  3753. obj = kzalloc(sizeof(*obj), GFP_KERNEL);
  3754. if (obj == NULL)
  3755. return NULL;
  3756. if (drm_gem_object_init(dev, &obj->base, size) != 0) {
  3757. kfree(obj);
  3758. return NULL;
  3759. }
  3760. obj->base.write_domain = I915_GEM_DOMAIN_CPU;
  3761. obj->base.read_domains = I915_GEM_DOMAIN_CPU;
  3762. obj->agp_type = AGP_USER_MEMORY;
  3763. obj->base.driver_private = NULL;
  3764. obj->fence_reg = I915_FENCE_REG_NONE;
  3765. INIT_LIST_HEAD(&obj->list);
  3766. INIT_LIST_HEAD(&obj->gpu_write_list);
  3767. obj->madv = I915_MADV_WILLNEED;
  3768. trace_i915_gem_object_create(&obj->base);
  3769. return &obj->base;
  3770. }
  3771. int i915_gem_init_object(struct drm_gem_object *obj)
  3772. {
  3773. BUG();
  3774. return 0;
  3775. }
  3776. void i915_gem_free_object(struct drm_gem_object *obj)
  3777. {
  3778. struct drm_device *dev = obj->dev;
  3779. struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
  3780. trace_i915_gem_object_destroy(obj);
  3781. while (obj_priv->pin_count > 0)
  3782. i915_gem_object_unpin(obj);
  3783. if (obj_priv->phys_obj)
  3784. i915_gem_detach_phys_object(dev, obj);
  3785. i915_gem_object_unbind(obj);
  3786. if (obj_priv->mmap_offset)
  3787. i915_gem_free_mmap_offset(obj);
  3788. drm_gem_object_release(obj);
  3789. kfree(obj_priv->page_cpu_valid);
  3790. kfree(obj_priv->bit_17);
  3791. kfree(obj_priv);
  3792. }
  3793. /** Unbinds all inactive objects. */
  3794. static int
  3795. i915_gem_evict_from_inactive_list(struct drm_device *dev)
  3796. {
  3797. drm_i915_private_t *dev_priv = dev->dev_private;
  3798. while (!list_empty(&dev_priv->mm.inactive_list)) {
  3799. struct drm_gem_object *obj;
  3800. int ret;
  3801. obj = &list_first_entry(&dev_priv->mm.inactive_list,
  3802. struct drm_i915_gem_object,
  3803. list)->base;
  3804. ret = i915_gem_object_unbind(obj);
  3805. if (ret != 0) {
  3806. DRM_ERROR("Error unbinding object: %d\n", ret);
  3807. return ret;
  3808. }
  3809. }
  3810. return 0;
  3811. }
  3812. int
  3813. i915_gem_idle(struct drm_device *dev)
  3814. {
  3815. drm_i915_private_t *dev_priv = dev->dev_private;
  3816. int ret;
  3817. mutex_lock(&dev->struct_mutex);
  3818. if (dev_priv->mm.suspended ||
  3819. (dev_priv->render_ring.gem_object == NULL) ||
  3820. (HAS_BSD(dev) &&
  3821. dev_priv->bsd_ring.gem_object == NULL)) {
  3822. mutex_unlock(&dev->struct_mutex);
  3823. return 0;
  3824. }
  3825. ret = i915_gpu_idle(dev);
  3826. if (ret) {
  3827. mutex_unlock(&dev->struct_mutex);
  3828. return ret;
  3829. }
  3830. /* Under UMS, be paranoid and evict. */
  3831. if (!drm_core_check_feature(dev, DRIVER_MODESET)) {
  3832. ret = i915_gem_evict_from_inactive_list(dev);
  3833. if (ret) {
  3834. mutex_unlock(&dev->struct_mutex);
  3835. return ret;
  3836. }
  3837. }
  3838. /* Hack! Don't let anybody do execbuf while we don't control the chip.
  3839. * We need to replace this with a semaphore, or something.
  3840. * And not confound mm.suspended!
  3841. */
  3842. dev_priv->mm.suspended = 1;
  3843. del_timer(&dev_priv->hangcheck_timer);
  3844. i915_kernel_lost_context(dev);
  3845. i915_gem_cleanup_ringbuffer(dev);
  3846. mutex_unlock(&dev->struct_mutex);
  3847. /* Cancel the retire work handler, which should be idle now. */
  3848. cancel_delayed_work_sync(&dev_priv->mm.retire_work);
  3849. return 0;
  3850. }
  3851. /*
  3852. * 965+ support PIPE_CONTROL commands, which provide finer grained control
  3853. * over cache flushing.
  3854. */
  3855. static int
  3856. i915_gem_init_pipe_control(struct drm_device *dev)
  3857. {
  3858. drm_i915_private_t *dev_priv = dev->dev_private;
  3859. struct drm_gem_object *obj;
  3860. struct drm_i915_gem_object *obj_priv;
  3861. int ret;
  3862. obj = i915_gem_alloc_object(dev, 4096);
  3863. if (obj == NULL) {
  3864. DRM_ERROR("Failed to allocate seqno page\n");
  3865. ret = -ENOMEM;
  3866. goto err;
  3867. }
  3868. obj_priv = to_intel_bo(obj);
  3869. obj_priv->agp_type = AGP_USER_CACHED_MEMORY;
  3870. ret = i915_gem_object_pin(obj, 4096);
  3871. if (ret)
  3872. goto err_unref;
  3873. dev_priv->seqno_gfx_addr = obj_priv->gtt_offset;
  3874. dev_priv->seqno_page = kmap(obj_priv->pages[0]);
  3875. if (dev_priv->seqno_page == NULL)
  3876. goto err_unpin;
  3877. dev_priv->seqno_obj = obj;
  3878. memset(dev_priv->seqno_page, 0, PAGE_SIZE);
  3879. return 0;
  3880. err_unpin:
  3881. i915_gem_object_unpin(obj);
  3882. err_unref:
  3883. drm_gem_object_unreference(obj);
  3884. err:
  3885. return ret;
  3886. }
  3887. static void
  3888. i915_gem_cleanup_pipe_control(struct drm_device *dev)
  3889. {
  3890. drm_i915_private_t *dev_priv = dev->dev_private;
  3891. struct drm_gem_object *obj;
  3892. struct drm_i915_gem_object *obj_priv;
  3893. obj = dev_priv->seqno_obj;
  3894. obj_priv = to_intel_bo(obj);
  3895. kunmap(obj_priv->pages[0]);
  3896. i915_gem_object_unpin(obj);
  3897. drm_gem_object_unreference(obj);
  3898. dev_priv->seqno_obj = NULL;
  3899. dev_priv->seqno_page = NULL;
  3900. }
  3901. int
  3902. i915_gem_init_ringbuffer(struct drm_device *dev)
  3903. {
  3904. drm_i915_private_t *dev_priv = dev->dev_private;
  3905. int ret;
  3906. dev_priv->render_ring = render_ring;
  3907. if (!I915_NEED_GFX_HWS(dev)) {
  3908. dev_priv->render_ring.status_page.page_addr
  3909. = dev_priv->status_page_dmah->vaddr;
  3910. memset(dev_priv->render_ring.status_page.page_addr,
  3911. 0, PAGE_SIZE);
  3912. }
  3913. if (HAS_PIPE_CONTROL(dev)) {
  3914. ret = i915_gem_init_pipe_control(dev);
  3915. if (ret)
  3916. return ret;
  3917. }
  3918. ret = intel_init_ring_buffer(dev, &dev_priv->render_ring);
  3919. if (ret)
  3920. goto cleanup_pipe_control;
  3921. if (HAS_BSD(dev)) {
  3922. dev_priv->bsd_ring = bsd_ring;
  3923. ret = intel_init_ring_buffer(dev, &dev_priv->bsd_ring);
  3924. if (ret)
  3925. goto cleanup_render_ring;
  3926. }
  3927. return 0;
  3928. cleanup_render_ring:
  3929. intel_cleanup_ring_buffer(dev, &dev_priv->render_ring);
  3930. cleanup_pipe_control:
  3931. if (HAS_PIPE_CONTROL(dev))
  3932. i915_gem_cleanup_pipe_control(dev);
  3933. return ret;
  3934. }
  3935. void
  3936. i915_gem_cleanup_ringbuffer(struct drm_device *dev)
  3937. {
  3938. drm_i915_private_t *dev_priv = dev->dev_private;
  3939. intel_cleanup_ring_buffer(dev, &dev_priv->render_ring);
  3940. if (HAS_BSD(dev))
  3941. intel_cleanup_ring_buffer(dev, &dev_priv->bsd_ring);
  3942. if (HAS_PIPE_CONTROL(dev))
  3943. i915_gem_cleanup_pipe_control(dev);
  3944. }
  3945. int
  3946. i915_gem_entervt_ioctl(struct drm_device *dev, void *data,
  3947. struct drm_file *file_priv)
  3948. {
  3949. drm_i915_private_t *dev_priv = dev->dev_private;
  3950. int ret;
  3951. if (drm_core_check_feature(dev, DRIVER_MODESET))
  3952. return 0;
  3953. if (atomic_read(&dev_priv->mm.wedged)) {
  3954. DRM_ERROR("Reenabling wedged hardware, good luck\n");
  3955. atomic_set(&dev_priv->mm.wedged, 0);
  3956. }
  3957. mutex_lock(&dev->struct_mutex);
  3958. dev_priv->mm.suspended = 0;
  3959. ret = i915_gem_init_ringbuffer(dev);
  3960. if (ret != 0) {
  3961. mutex_unlock(&dev->struct_mutex);
  3962. return ret;
  3963. }
  3964. spin_lock(&dev_priv->mm.active_list_lock);
  3965. BUG_ON(!list_empty(&dev_priv->render_ring.active_list));
  3966. BUG_ON(HAS_BSD(dev) && !list_empty(&dev_priv->bsd_ring.active_list));
  3967. spin_unlock(&dev_priv->mm.active_list_lock);
  3968. BUG_ON(!list_empty(&dev_priv->mm.flushing_list));
  3969. BUG_ON(!list_empty(&dev_priv->mm.inactive_list));
  3970. BUG_ON(!list_empty(&dev_priv->render_ring.request_list));
  3971. BUG_ON(HAS_BSD(dev) && !list_empty(&dev_priv->bsd_ring.request_list));
  3972. mutex_unlock(&dev->struct_mutex);
  3973. drm_irq_install(dev);
  3974. return 0;
  3975. }
  3976. int
  3977. i915_gem_leavevt_ioctl(struct drm_device *dev, void *data,
  3978. struct drm_file *file_priv)
  3979. {
  3980. if (drm_core_check_feature(dev, DRIVER_MODESET))
  3981. return 0;
  3982. drm_irq_uninstall(dev);
  3983. return i915_gem_idle(dev);
  3984. }
  3985. void
  3986. i915_gem_lastclose(struct drm_device *dev)
  3987. {
  3988. int ret;
  3989. if (drm_core_check_feature(dev, DRIVER_MODESET))
  3990. return;
  3991. ret = i915_gem_idle(dev);
  3992. if (ret)
  3993. DRM_ERROR("failed to idle hardware: %d\n", ret);
  3994. }
  3995. void
  3996. i915_gem_load(struct drm_device *dev)
  3997. {
  3998. int i;
  3999. drm_i915_private_t *dev_priv = dev->dev_private;
  4000. spin_lock_init(&dev_priv->mm.active_list_lock);
  4001. INIT_LIST_HEAD(&dev_priv->mm.flushing_list);
  4002. INIT_LIST_HEAD(&dev_priv->mm.gpu_write_list);
  4003. INIT_LIST_HEAD(&dev_priv->mm.inactive_list);
  4004. INIT_LIST_HEAD(&dev_priv->mm.fence_list);
  4005. INIT_LIST_HEAD(&dev_priv->render_ring.active_list);
  4006. INIT_LIST_HEAD(&dev_priv->render_ring.request_list);
  4007. if (HAS_BSD(dev)) {
  4008. INIT_LIST_HEAD(&dev_priv->bsd_ring.active_list);
  4009. INIT_LIST_HEAD(&dev_priv->bsd_ring.request_list);
  4010. }
  4011. for (i = 0; i < 16; i++)
  4012. INIT_LIST_HEAD(&dev_priv->fence_regs[i].lru_list);
  4013. INIT_DELAYED_WORK(&dev_priv->mm.retire_work,
  4014. i915_gem_retire_work_handler);
  4015. spin_lock(&shrink_list_lock);
  4016. list_add(&dev_priv->mm.shrink_list, &shrink_list);
  4017. spin_unlock(&shrink_list_lock);
  4018. /* On GEN3 we really need to make sure the ARB C3 LP bit is set */
  4019. if (IS_GEN3(dev)) {
  4020. u32 tmp = I915_READ(MI_ARB_STATE);
  4021. if (!(tmp & MI_ARB_C3_LP_WRITE_ENABLE)) {
  4022. /* arb state is a masked write, so set bit + bit in mask */
  4023. tmp = MI_ARB_C3_LP_WRITE_ENABLE | (MI_ARB_C3_LP_WRITE_ENABLE << MI_ARB_MASK_SHIFT);
  4024. I915_WRITE(MI_ARB_STATE, tmp);
  4025. }
  4026. }
  4027. /* Old X drivers will take 0-2 for front, back, depth buffers */
  4028. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  4029. dev_priv->fence_reg_start = 3;
  4030. if (IS_I965G(dev) || IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
  4031. dev_priv->num_fence_regs = 16;
  4032. else
  4033. dev_priv->num_fence_regs = 8;
  4034. /* Initialize fence registers to zero */
  4035. if (IS_I965G(dev)) {
  4036. for (i = 0; i < 16; i++)
  4037. I915_WRITE64(FENCE_REG_965_0 + (i * 8), 0);
  4038. } else {
  4039. for (i = 0; i < 8; i++)
  4040. I915_WRITE(FENCE_REG_830_0 + (i * 4), 0);
  4041. if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
  4042. for (i = 0; i < 8; i++)
  4043. I915_WRITE(FENCE_REG_945_8 + (i * 4), 0);
  4044. }
  4045. i915_gem_detect_bit_6_swizzle(dev);
  4046. init_waitqueue_head(&dev_priv->pending_flip_queue);
  4047. }
  4048. /*
  4049. * Create a physically contiguous memory object for this object
  4050. * e.g. for cursor + overlay regs
  4051. */
  4052. int i915_gem_init_phys_object(struct drm_device *dev,
  4053. int id, int size)
  4054. {
  4055. drm_i915_private_t *dev_priv = dev->dev_private;
  4056. struct drm_i915_gem_phys_object *phys_obj;
  4057. int ret;
  4058. if (dev_priv->mm.phys_objs[id - 1] || !size)
  4059. return 0;
  4060. phys_obj = kzalloc(sizeof(struct drm_i915_gem_phys_object), GFP_KERNEL);
  4061. if (!phys_obj)
  4062. return -ENOMEM;
  4063. phys_obj->id = id;
  4064. phys_obj->handle = drm_pci_alloc(dev, size, 0);
  4065. if (!phys_obj->handle) {
  4066. ret = -ENOMEM;
  4067. goto kfree_obj;
  4068. }
  4069. #ifdef CONFIG_X86
  4070. set_memory_wc((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE);
  4071. #endif
  4072. dev_priv->mm.phys_objs[id - 1] = phys_obj;
  4073. return 0;
  4074. kfree_obj:
  4075. kfree(phys_obj);
  4076. return ret;
  4077. }
  4078. void i915_gem_free_phys_object(struct drm_device *dev, int id)
  4079. {
  4080. drm_i915_private_t *dev_priv = dev->dev_private;
  4081. struct drm_i915_gem_phys_object *phys_obj;
  4082. if (!dev_priv->mm.phys_objs[id - 1])
  4083. return;
  4084. phys_obj = dev_priv->mm.phys_objs[id - 1];
  4085. if (phys_obj->cur_obj) {
  4086. i915_gem_detach_phys_object(dev, phys_obj->cur_obj);
  4087. }
  4088. #ifdef CONFIG_X86
  4089. set_memory_wb((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE);
  4090. #endif
  4091. drm_pci_free(dev, phys_obj->handle);
  4092. kfree(phys_obj);
  4093. dev_priv->mm.phys_objs[id - 1] = NULL;
  4094. }
  4095. void i915_gem_free_all_phys_object(struct drm_device *dev)
  4096. {
  4097. int i;
  4098. for (i = I915_GEM_PHYS_CURSOR_0; i <= I915_MAX_PHYS_OBJECT; i++)
  4099. i915_gem_free_phys_object(dev, i);
  4100. }
  4101. void i915_gem_detach_phys_object(struct drm_device *dev,
  4102. struct drm_gem_object *obj)
  4103. {
  4104. struct drm_i915_gem_object *obj_priv;
  4105. int i;
  4106. int ret;
  4107. int page_count;
  4108. obj_priv = to_intel_bo(obj);
  4109. if (!obj_priv->phys_obj)
  4110. return;
  4111. ret = i915_gem_object_get_pages(obj, 0);
  4112. if (ret)
  4113. goto out;
  4114. page_count = obj->size / PAGE_SIZE;
  4115. for (i = 0; i < page_count; i++) {
  4116. char *dst = kmap_atomic(obj_priv->pages[i], KM_USER0);
  4117. char *src = obj_priv->phys_obj->handle->vaddr + (i * PAGE_SIZE);
  4118. memcpy(dst, src, PAGE_SIZE);
  4119. kunmap_atomic(dst, KM_USER0);
  4120. }
  4121. drm_clflush_pages(obj_priv->pages, page_count);
  4122. drm_agp_chipset_flush(dev);
  4123. i915_gem_object_put_pages(obj);
  4124. out:
  4125. obj_priv->phys_obj->cur_obj = NULL;
  4126. obj_priv->phys_obj = NULL;
  4127. }
  4128. int
  4129. i915_gem_attach_phys_object(struct drm_device *dev,
  4130. struct drm_gem_object *obj, int id)
  4131. {
  4132. drm_i915_private_t *dev_priv = dev->dev_private;
  4133. struct drm_i915_gem_object *obj_priv;
  4134. int ret = 0;
  4135. int page_count;
  4136. int i;
  4137. if (id > I915_MAX_PHYS_OBJECT)
  4138. return -EINVAL;
  4139. obj_priv = to_intel_bo(obj);
  4140. if (obj_priv->phys_obj) {
  4141. if (obj_priv->phys_obj->id == id)
  4142. return 0;
  4143. i915_gem_detach_phys_object(dev, obj);
  4144. }
  4145. /* create a new object */
  4146. if (!dev_priv->mm.phys_objs[id - 1]) {
  4147. ret = i915_gem_init_phys_object(dev, id,
  4148. obj->size);
  4149. if (ret) {
  4150. DRM_ERROR("failed to init phys object %d size: %zu\n", id, obj->size);
  4151. goto out;
  4152. }
  4153. }
  4154. /* bind to the object */
  4155. obj_priv->phys_obj = dev_priv->mm.phys_objs[id - 1];
  4156. obj_priv->phys_obj->cur_obj = obj;
  4157. ret = i915_gem_object_get_pages(obj, 0);
  4158. if (ret) {
  4159. DRM_ERROR("failed to get page list\n");
  4160. goto out;
  4161. }
  4162. page_count = obj->size / PAGE_SIZE;
  4163. for (i = 0; i < page_count; i++) {
  4164. char *src = kmap_atomic(obj_priv->pages[i], KM_USER0);
  4165. char *dst = obj_priv->phys_obj->handle->vaddr + (i * PAGE_SIZE);
  4166. memcpy(dst, src, PAGE_SIZE);
  4167. kunmap_atomic(src, KM_USER0);
  4168. }
  4169. i915_gem_object_put_pages(obj);
  4170. return 0;
  4171. out:
  4172. return ret;
  4173. }
  4174. static int
  4175. i915_gem_phys_pwrite(struct drm_device *dev, struct drm_gem_object *obj,
  4176. struct drm_i915_gem_pwrite *args,
  4177. struct drm_file *file_priv)
  4178. {
  4179. struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
  4180. void *obj_addr;
  4181. int ret;
  4182. char __user *user_data;
  4183. user_data = (char __user *) (uintptr_t) args->data_ptr;
  4184. obj_addr = obj_priv->phys_obj->handle->vaddr + args->offset;
  4185. DRM_DEBUG_DRIVER("obj_addr %p, %lld\n", obj_addr, args->size);
  4186. ret = copy_from_user(obj_addr, user_data, args->size);
  4187. if (ret)
  4188. return -EFAULT;
  4189. drm_agp_chipset_flush(dev);
  4190. return 0;
  4191. }
  4192. void i915_gem_release(struct drm_device * dev, struct drm_file *file_priv)
  4193. {
  4194. struct drm_i915_file_private *i915_file_priv = file_priv->driver_priv;
  4195. /* Clean up our request list when the client is going away, so that
  4196. * later retire_requests won't dereference our soon-to-be-gone
  4197. * file_priv.
  4198. */
  4199. mutex_lock(&dev->struct_mutex);
  4200. while (!list_empty(&i915_file_priv->mm.request_list))
  4201. list_del_init(i915_file_priv->mm.request_list.next);
  4202. mutex_unlock(&dev->struct_mutex);
  4203. }
  4204. static int
  4205. i915_gpu_is_active(struct drm_device *dev)
  4206. {
  4207. drm_i915_private_t *dev_priv = dev->dev_private;
  4208. int lists_empty;
  4209. spin_lock(&dev_priv->mm.active_list_lock);
  4210. lists_empty = list_empty(&dev_priv->mm.flushing_list) &&
  4211. list_empty(&dev_priv->render_ring.active_list);
  4212. if (HAS_BSD(dev))
  4213. lists_empty &= list_empty(&dev_priv->bsd_ring.active_list);
  4214. spin_unlock(&dev_priv->mm.active_list_lock);
  4215. return !lists_empty;
  4216. }
  4217. static int
  4218. i915_gem_shrink(struct shrinker *shrink, int nr_to_scan, gfp_t gfp_mask)
  4219. {
  4220. drm_i915_private_t *dev_priv, *next_dev;
  4221. struct drm_i915_gem_object *obj_priv, *next_obj;
  4222. int cnt = 0;
  4223. int would_deadlock = 1;
  4224. /* "fast-path" to count number of available objects */
  4225. if (nr_to_scan == 0) {
  4226. spin_lock(&shrink_list_lock);
  4227. list_for_each_entry(dev_priv, &shrink_list, mm.shrink_list) {
  4228. struct drm_device *dev = dev_priv->dev;
  4229. if (mutex_trylock(&dev->struct_mutex)) {
  4230. list_for_each_entry(obj_priv,
  4231. &dev_priv->mm.inactive_list,
  4232. list)
  4233. cnt++;
  4234. mutex_unlock(&dev->struct_mutex);
  4235. }
  4236. }
  4237. spin_unlock(&shrink_list_lock);
  4238. return (cnt / 100) * sysctl_vfs_cache_pressure;
  4239. }
  4240. spin_lock(&shrink_list_lock);
  4241. rescan:
  4242. /* first scan for clean buffers */
  4243. list_for_each_entry_safe(dev_priv, next_dev,
  4244. &shrink_list, mm.shrink_list) {
  4245. struct drm_device *dev = dev_priv->dev;
  4246. if (! mutex_trylock(&dev->struct_mutex))
  4247. continue;
  4248. spin_unlock(&shrink_list_lock);
  4249. i915_gem_retire_requests(dev, &dev_priv->render_ring);
  4250. if (HAS_BSD(dev))
  4251. i915_gem_retire_requests(dev, &dev_priv->bsd_ring);
  4252. list_for_each_entry_safe(obj_priv, next_obj,
  4253. &dev_priv->mm.inactive_list,
  4254. list) {
  4255. if (i915_gem_object_is_purgeable(obj_priv)) {
  4256. i915_gem_object_unbind(&obj_priv->base);
  4257. if (--nr_to_scan <= 0)
  4258. break;
  4259. }
  4260. }
  4261. spin_lock(&shrink_list_lock);
  4262. mutex_unlock(&dev->struct_mutex);
  4263. would_deadlock = 0;
  4264. if (nr_to_scan <= 0)
  4265. break;
  4266. }
  4267. /* second pass, evict/count anything still on the inactive list */
  4268. list_for_each_entry_safe(dev_priv, next_dev,
  4269. &shrink_list, mm.shrink_list) {
  4270. struct drm_device *dev = dev_priv->dev;
  4271. if (! mutex_trylock(&dev->struct_mutex))
  4272. continue;
  4273. spin_unlock(&shrink_list_lock);
  4274. list_for_each_entry_safe(obj_priv, next_obj,
  4275. &dev_priv->mm.inactive_list,
  4276. list) {
  4277. if (nr_to_scan > 0) {
  4278. i915_gem_object_unbind(&obj_priv->base);
  4279. nr_to_scan--;
  4280. } else
  4281. cnt++;
  4282. }
  4283. spin_lock(&shrink_list_lock);
  4284. mutex_unlock(&dev->struct_mutex);
  4285. would_deadlock = 0;
  4286. }
  4287. if (nr_to_scan) {
  4288. int active = 0;
  4289. /*
  4290. * We are desperate for pages, so as a last resort, wait
  4291. * for the GPU to finish and discard whatever we can.
  4292. * This has a dramatic impact to reduce the number of
  4293. * OOM-killer events whilst running the GPU aggressively.
  4294. */
  4295. list_for_each_entry(dev_priv, &shrink_list, mm.shrink_list) {
  4296. struct drm_device *dev = dev_priv->dev;
  4297. if (!mutex_trylock(&dev->struct_mutex))
  4298. continue;
  4299. spin_unlock(&shrink_list_lock);
  4300. if (i915_gpu_is_active(dev)) {
  4301. i915_gpu_idle(dev);
  4302. active++;
  4303. }
  4304. spin_lock(&shrink_list_lock);
  4305. mutex_unlock(&dev->struct_mutex);
  4306. }
  4307. if (active)
  4308. goto rescan;
  4309. }
  4310. spin_unlock(&shrink_list_lock);
  4311. if (would_deadlock)
  4312. return -1;
  4313. else if (cnt > 0)
  4314. return (cnt / 100) * sysctl_vfs_cache_pressure;
  4315. else
  4316. return 0;
  4317. }
  4318. static struct shrinker shrinker = {
  4319. .shrink = i915_gem_shrink,
  4320. .seeks = DEFAULT_SEEKS,
  4321. };
  4322. __init void
  4323. i915_gem_shrinker_init(void)
  4324. {
  4325. register_shrinker(&shrinker);
  4326. }
  4327. __exit void
  4328. i915_gem_shrinker_exit(void)
  4329. {
  4330. unregister_shrinker(&shrinker);
  4331. }